In PHP, is there a difference between Double and Float?

Asked

Viewed 2,391 times

3

In some programming languages, Float and Double, although similar, are not the same. But apparently in PHP there is no difference between these two types.

Is there any special reason for this? Because the cast of Double and Float result in the same thing?

 gettype( (double) '1.66' ); // double

 gettype( (float) '1.66' ); // double

 is_double(1.66); // true

 is_float(1.66); // true
  • There’s really no difference in all three. Related issue in Soen

  • Forgot the question or haven’t found the answer you wanted? hahaha

2 answers

5

To complete the francis' answer, it is valid to put the C source code of the PHP implementation. For example, when using the function settype, the documentation recommends:

Possible values for type are:

  • "float" (only for versions above PHP 4.2.0, for older versions use the deprecated variation "double")

That is, you will need to define how float instead of double, but checking the implementation of the function, we have:

/* {{{ proto bool settype(mixed &var, string type)
   Set the type of the variable */
PHP_FUNCTION(settype)
{
    zval *var;
    char *type;
    size_t type_len = 0;

    ZEND_PARSE_PARAMETERS_START(2, 2)
        Z_PARAM_ZVAL_DEREF(var)
        Z_PARAM_STRING(type, type_len)
    ZEND_PARSE_PARAMETERS_END();

    if (!strcasecmp(type, "integer")) {
        convert_to_long(var);
    } else if (!strcasecmp(type, "int")) {
        convert_to_long(var);
    } else if (!strcasecmp(type, "float")) {
        convert_to_double(var);
    } else if (!strcasecmp(type, "double")) { /* deprecated */
        convert_to_double(var);
    } else if (!strcasecmp(type, "string")) {
        convert_to_string(var);
    } else if (!strcasecmp(type, "array")) {
        convert_to_array(var);
    } else if (!strcasecmp(type, "object")) {
        convert_to_object(var);
    } else if (!strcasecmp(type, "bool")) {
        convert_to_boolean(var);
    } else if (!strcasecmp(type, "boolean")) {
        convert_to_boolean(var);
    } else if (!strcasecmp(type, "null")) {
        convert_to_null(var);
    } else if (!strcasecmp(type, "resource")) {
        php_error_docref(NULL, E_WARNING, "Cannot convert to resource type");
        RETURN_FALSE;
    } else {
        php_error_docref(NULL, E_WARNING, "Invalid type");
        RETURN_FALSE;
    }
    RETVAL_TRUE;
}
/* }}} */

Where the lines stand out:

} else if (!strcasecmp(type, "float")) {
    convert_to_double(var);
}

If the guy is float, converts the value to double. And the type defined as double was rendered obsolete for who knows why (searching for information in the changelog).

The same occurs with the function floatval:

/* {{{ proto float floatval(mixed var)
   Get the float value of a variable */
PHP_FUNCTION(floatval)
{
    zval *num;

    ZEND_PARSE_PARAMETERS_START(1, 1)
        Z_PARAM_ZVAL(num)
    ZEND_PARSE_PARAMETERS_END();

    RETURN_DOUBLE(zval_get_double(num));
}
/* }}} */

Returning the very type double.

The full source file can be seen on official language repository.

4


There is a difference between Double and Float or Real?

No, there’s no difference between double, float or real in php.

Is there any special reason for it?

Yes! Practicality and to simplify things too. In C, which is where PHP is made, the variables are stored in a structure, which consists of a union between various types of data that the variable could store, such as boolean, string, int, and double, which is how all values with decimals are stored, regardless of whether it was considered double, float or real, in php.

You can see more about this here.

Because the cast of Double and Float result in the same thing?

As I said in the answer above, behind the scenes (in C), everything ends up being transformed into double.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.