|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-03-29 01:05 UTC] t dot steve at ariadne-quatra dot com
Description: ------------ Subtraction does not work as expected. Windows 2000 Server SP4 IIS5 PHP5RC1 Reproduce code: --------------- $result=141.23-141.00; echo $result; (or $result=141.23-141; echo $result; - same result) Expected result: ---------------- 0.23 Actual result: -------------- 0.22999999999999 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 08:00:01 2025 UTC |
Guys, how in the world is PHP supposed to magically guess what precision you want results displayed in. If you know you always want lower precision, set that in your php.ini file. Or if you just want it temporarily simply do: $old = ini_set('precision',2); echo (string)(750 - 749.99); ini_set('precision',$old);I'm sorry I keep posting :) (Maybe I'm just stupid. This will be my last post, maybe I'm plain wrong.) >Reducing precision during the calculation would magnify >rounding errors greatly when doing a series of operations >on the floating point values. Yes. But for subtraction, the difference can never have "more decimals" (precision) than either its minuend or subtrahend, anything else introduces more error in multiple operations. Or? In C++, if you substract a float (750.00f) from a float (749.99f), it will result in 0.0100098. *However*, if you do the same with double, insted of floats, it would result in 0.01 (I think). The first one, float substraction, is probably faster, the second is more "accurate". (I don't think that type of speed optimization is relevant to a php-script?) Another option is ((a * 100) - (b * 100)) / 100; "100" would have to be modified to the "greatest" precision of either a or b. This will not "magnify rounding errors", because it will be exact. I'm only talking about subtraction. What I'm looking for is somthing like "AUTO_PRECISION = yes" and it would do something like: $old = ini_set('precision', max_decimals(750, 749.99)); echo (string)(750 - 749.99); ini_set('precision',$old); or use a C++ double instead of float (?). Thanks for your patience! :)