note 119583 deleted from language.types.float by cmb

From: Date: Mon, 11 Jul 2016 22:09:05 +0000
Subject: note 119583 deleted from language.types.float by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-205974@lists.php.net to get a copy of this message
Note Submitter: Leandro Scott ---- 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.

« previous php.notes (#205974) next »