What is the maximum of decimal places allowed in the PHP float?

Asked

Viewed 1,596 times

2

I was using Psysh to test with numbers like float.

I noticed that, after a certain amount, it begins to limit the decimal places.

Example:

 $float = 1.1234567891011121314

 echo $float; //  1.1234567891011

Why does this happen? Is there a maximum supported? If so, which is the maximum supported?

If I need the full number, how do I get it?

1 answer

6

Floating point numbers have limited accuracy. Although it relies on the system, PHP generally uses the IEEE 754 double-precision format, which will bring maximum accuracy due to rounding of the order of 1.11e-16. Unusual mathematical operations may cause larger errors, and of course the propagation of errors should be considered when several operations are performed.

Source: http://php.net/manual/en/language.types.float.php

The IEEE 754 standard (defined by the Institute of Electrical Engineers and Electronics) was adopted in 1985 and has since gone through some modifications, and defines some standards to be followed in operations and representations of binary numbers with point floating. Before that, each computer manufacturer and others devices, had a different representation format.

As for the accuracy of numerical representation, the main ones are:

Simple

32 bits or single accuracy (float), equivalent to up to 7 decimal digits.

1 bit for signal.

8 bits for the exponent.

23 bits for the mantissa representation.

Duo

64 bits or double precision (double), equivalent to up to 15 decimal digits.

1 bit intended for signal;

11 bits intended for the exponent;

52 bits intended for mantissa.

Source: https://pt.wikipedia.org/wiki/IEEE_754

  • And what I need to do to return the full number in this case?

  • 1

    Have you tried using round?

  • That’s not what I meant, but let’s do it. I’ll settle this with number_format. Thanks

Browser other questions tagged

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