note 119583 added to language.types.float
| From: | LeandroScott | Date: | Mon, 11 Jul 2016 19:33:06 +0000 |
| Subject: | note 119583 added to language.types.float | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205969@lists.php.net to get a copy of this message | ||
Hint to overcome some imprecisions during various subtractions in sequence. I was making a sequence
of subtractions and I expected 0 as a result and I had put an if statement asking if the variable
value was >= 0, for my surprise it gave me a negative number. The subtraction was 0.03 - 0.01 -
0.005 - 0.005 - 0.01 and the result was -5.2041704279304E-18. So I had to make a sum to subtract
later, like this:
<?php
$a = 0.03
$b = 0.01 + 0.005 + 0.005 + 0.01
$res = $a - $b
?>
So, finally, I could get the right result I didn't expect to get floating point precision
problems with this simple subtraction. It was a pain to solve the problem, because you never
remember that floating point math has it's issues.
----
Server IP: 72.52.91.14
Probable Submitter: 191.251.23.214
----
Manual Page -- http://php.net/manual/en/language.types.float.php
Edit -- https://master.php.net/note/edit/119583
Del: integrated -- https://master.php.net/note/delete/119583/integrated
Del: useless -- https://master.php.net/note/delete/119583/useless
Del: bad code -- https://master.php.net/note/delete/119583/bad+code
Del: spam -- https://master.php.net/note/delete/119583/spam
Del: non-english -- https://master.php.net/note/delete/119583/non-english
Del: in docs -- https://master.php.net/note/delete/119583/in+docs
Del: other reasons-- https://master.php.net/note/delete/119583
Reject -- https://master.php.net/note/reject/119583
Search -- https://master.php.net/manage/user-notes.php