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

2

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

Is there any special reason for this? Why does Cast 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
    
asked by anonymous 05.08.2017 / 19:28

2 answers

4
  

Is there a difference between Double and Float or Real?

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

  

Is there any special reason for this?

Yes! Practicality and to simplify things as well. In C , which is where PHP is done, 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 decimal places are stored, regardless of whether it was double , float or real , in php.

You can read more about this here .

  

Why does Double casting and Float result in the same thing?

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

    
05.08.2017 / 20:09
5

To complete Francisco's response , it is valid to place the C source code of the PHP implementation. For example, when using settype , the documentation recommends:

  

The possible values for type are:

     
  • "float" (for versions above PHP 4.2.0 only, for old versions use the depreciated variant "double")
  •   

That is, you need to set it to 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 are highlighted:

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

If the type is float , it converts the value to double . And the type defined as double has been rendered obsolete know why (fetch the information in the changelog ).

The same thing happens with the floatval function:

/* {{{ 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 type itself double .

  

The complete source file can be seen in the official language repository >.

    
05.08.2017 / 20:52