Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Simple adding numbers

by nvivek (Vicar)
on Mar 26, 2013 at 05:03 UTC ( [id://1025434]=note: print w/replies, xml ) Need Help??


in reply to Simple adding numbers

When I used lt instead of < operator, it works as you expected. Still, lt operator used for comparing strings not for numerals. As per my understanding, perl tries to implicitly convert both $i and 1 as strings and do comparison. If somebody finds different reason for it, kindly clarify me.

my $range = 0.1; my $i = 0; while( $i lt 1 ) { print $i."\n"; $i = $i + $range; }

Replies are listed 'Best First'.
Re^2: Simple adding numbers
by davido (Cardinal) on Mar 26, 2013 at 05:38 UTC

    perl -E say 'Ouch!' if '0002' lt 1;

    Ouch!

    I would recommend caution if you're going to go down that road. The best thing to do is to simply understand the nature of floating point precision and deal with it in the most straight-forward way possible. Stringification has its uses, but bending the rules of numeric relational comparisons is probably not an ideal one.


    Dave

Log In?
Username:
Password:

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

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

    No recent polls found