in reply to Re: Okay! What!?!?!?
in thread Okay! What!?!?!?

I understand that 'eq' is better, but this question came from a horribly designed test and the '==' operator has to be used.

I think they should be equal but the code isn't coming out as equal?

So for this test ( actual job test question ), what's the proper answer?

Replies are listed 'Best First'.
Re^3: Okay! What!?!?!?
by LanX (Saint) on Apr 23, 2014 at 01:22 UTC
    Sorry for misreading your question.

    It's (again) a floating point problem.

    DB<149> $a=14.4 => "14.4" DB<150> $b=10+$a-10 => "14.4" DB<151> $a-$b => "1.77635683940025e-15"

    The mantissa does loose some digits at the end after adding 10 and substracting doesn't bring them back.

    A proper test is either to stay integer from the beginning till the end or to calculate the difference and to compare against a threshold (trickier).

    Please note how 14.5 doesn't have this problem since 1/2 is a power of 2 (i.e. only 0s are lost when shifting the mantissa)

    update
    see Re: eq vs == and Humans have too many fingers for more details

    Cheers Rolf

    ( addicted to the Perl Programming Language)