Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Okay! What!?!?!?

by awohld (Hermit)
on Apr 23, 2014 at 00:53 UTC ( [id://1083249]=note: print w/replies, xml ) Need Help??


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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1083249]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found