0

I'm currently trying to do math with large decimals. This works fine:

<?php
$math = 99 + 0.0001;
echo $math;
?>

Output: 99.0001

However, when I try to add with decimals with more than 12 decimal places, I do not receive the expected output:

<?php
$math = 99 + 0.0000000000001;
echo $math;
?>

Output: 99

How can I make it so that if I add with a decimal that has more than 12 decimal places, that the result will still have the exact answer without rounding? For example:

<?php
$math = 99 + 0.0000000000001;
echo $math;
?>

Output: 99.0000000000001
user3681788
  • 221
  • 4
  • 15
  • 1
    Use [BC Math](http://us2.php.net/manual/en/book.bc.php) – John Conde Jun 27 '14 at 13:48
  • By using sprint() or adjusting the precision setting in php.ini to prevent PHP rounding when it displays – Mark Baker Jun 27 '14 at 13:52
  • http://stackoverflow.com/questions/670662/whats-the-maximum-size-for-an-int-in-php – samayo Jun 27 '14 at 13:52
  • 1
    @BiVOC - not really sure that this question has anything to do with maximum size of integers, poster is asking about floating point values – Mark Baker Jun 27 '14 at 13:53
  • What is your application? If your doing scientific calculations or something like 3D geometry then it may not matter - 12 significant figure is more than enough, just use printf to format. If you are prepared to sacrifice speed for precision use BC Math. – Salix alba Jun 27 '14 at 15:13

1 Answers1

0

Quick google search yielded this.

Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16.

The page I linked has lots of helpful info regarding floating point numbers in PHP as well as links to libraries which can work with arbitrary precision floating point numbers. Could be useful depending on your needs.

Edit: Also, as Mark Baker said in a comment, you may also need to specify the precision of the number you want to print in the printf() format string. Check this page out and look at number 5.

SyntaxTerror
  • 346
  • 1
  • 10
  • Be aware that the internal numeric precision (64 bit double vs 32 bit single vs 128 bit long double) is a different matter than the number of digits or precision of the print formatting. Obviously, you can't format what's not there in the original raw number, as well as, you can have tons of internal precision but if you don't format to enough decimal places... Welcome to the wonderful world of Numerical Analysis 101! – Phil Perry Jun 27 '14 at 14:15
  • @PhilPerry: PHP's `zval` uses a `double` to store decimals, which -by definition- is a double precision float in C. Since PHP is written in C, it's only fair to assume that a float is 64bit double precision. The C standard doesn't specify a `long double`. It could be the same as a `double`, it could be an 80bit precision data type, or the IEEE quadruple 128bit format. You just can't be sure – Elias Van Ootegem Jun 27 '14 at 14:24
  • The point still stands that you have to be aware of what the internal representation of a number is capable of storing, as well as how you are formatting the number for presentation. You also need to be aware of the effects of mathematical operations on numbers that are very far apart in range, or even very close in value. That's all elementary NA. – Phil Perry Jun 27 '14 at 14:37