Topic: Strangest bug I've ever since

I don't know how to explain this. I'm still confused with this bug. I have some strange bug and I'm trying to debugging until I found the root problem. I've just try some simple substraction but it gives me strange result. Here is my code:

$x = 0.56*1.5;
logger($x-0.84);

What I expection is 0, but the result is : 1.1102230246252E-16

If you want to know what is logger function, it's some function that I create to echo the parameter. The stranger thing is, this bug only happen to 2 decimal place operation. If I try with

$x = 0.5*1.5;
logger($x-0.75);

It works fine. I already check the variable type. Both show me they are double. Can anyone hlep me?

Re: Strangest bug I've ever since

I'm not a programmer so don't be insulted if this doesn't help you but you simply have a rounding issue. 1.1102230246252E-16 is technically 0.00000000000000001102230246252 which for anyone's purpose is 0.

I'm not sure if you have something in the logger function that could be doing it but could you just add an operation to truncate some decimal places before the final result? Sorry if that doesn't help.

Re: Strangest bug I've ever since

Yes, it's the rounding issue. But, I already print the $x which is exactly same as 0.84.

Re: Strangest bug I've ever since

Yes, it's about precision. The way how computer store floating point. See here for
http://www.leaseweblabs.com/2013/06/the … y-default/

and here
http://www.php.net/manual/en/language.types.float.php

lesson learned from php.net link:
"So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality."