Edit report at http://bugs.php.net/bug.php?id=52405&edit=1

 ID:               52405
 Updated by:       ahar...@php.net
 Reported by:      chrisbabar at hotmail dot com
 Summary:          Inaccurate Pi calculation
 Status:           Bogus
 Type:             Bug
 Package:          *Math Functions
 Operating System: Ubuntu 10.04 LTS
 PHP Version:      5.3.2

 New Comment:

Sorry, I was being a bit slack: we get enough floating point questions
that my first comment's actually a canned answer. :)



You're exactly right: it's just that floating point values don't have
enough precision to go beyond a certain point -- the float page in the
manual says it's usually about 14 decimal digits, which lines up almost
exactly with what you've just tested.



As for whether PHP should be limited, it's basically a reality of
current computing that decimal values are represented as floating point
values with limited precision -- fixed point arithmetic would
potentially be more accurate, but much, much slower, since you'd no
longer be getting any help from the hardware FPU, and for most purposes,
64-bit floating point values are good enough.


Previous Comments:
------------------------------------------------------------------------
[2010-07-22 16:04:05] chrisbabar at hotmail dot com

I think I understand what you are saying - however this is essentially
the same problem - without any float to string processing:



<?php

// Outputs bool(true)

var_dump(3.14159265358979311599796 === 3.14159265358979323846264);

?>



And if this is expected because of the limitations with the precision -
should the precision of PHP be limited to 15 decimal places?

------------------------------------------------------------------------
[2010-07-22 15:50:43] ahar...@php.net

Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.



------------------------------------------------------------------------
[2010-07-22 15:44:47] chrisbabar at hotmail dot com

Description:
------------
PHP seems to calculate pi incorrectly after 15 decimal places when
calling the pi() function. I have used http://en.wikipedia.org/wiki/Pi
as a reference for the correct value of pi.

Test script:
---------------
<?php



ini_set("precision", 24);



echo pi();



?>

Expected result:
----------------
3.14159265358979323846264

Actual result:
--------------
3.14159265358979311599796


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52405&edit=1

Reply via email to