Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: Re: A C-like brain in a Perl-like world

by ton (Friar)
on Sep 27, 2001 at 05:31 UTC ( [id://114972]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: A C-like brain in a Perl-like world
in thread A C-like brain in a Perl-like world

Apparently Perl is not as seemless in its conversions between strings and floating point numbers as it is in its conversions between strings and integers. Because the following works:

% perl -le '$foo=0.0; print $foo ? "True" : "False";' False
but the following does not:
% perl -le '$foo="0.0"; print $foo ? "True" : "False";' True

-----
Be bloody, bold, and resolute; laugh to scorn
The power of man...

Replies are listed 'Best First'.
Re: Re: Re: Re: A C-like brain in a Perl-like world
by $code or die (Deacon) on Sep 27, 2001 at 05:52 UTC
    Perl seems to be assuming that "0.0" is a string, which is fair enough, if that is by design, which I assume it is. "return 0.0" would from a subroutine would evaluate as false.

    Perl will convert "0.0" to a number as soon as you do anything mathematical on it. Such as:
    perl -e '$foo = "0.0" + 0; print $foo ? "True" : "False"'
    Which behaves as you'd expect. This reminds me of (even though it's completely different):
    perl -e 'print "I cannot add" unless 19.08 + 2.01 == 21.09'
    If I remember correctly this comes from an early TPJ article.

    Update: removed ' from print statement - thanks tilly - That's me in hybrid Win32 cmd.exe, Linux shell quoting mode :)

    Simon Flack ($code or die)
    $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
    =~y'_"' ';eval"die";print $_,lc substr$@,0,3;
      You need to do one of the following to make that work:
      perl -e 'print "I cannot add" unless 19.08 + 2.01 == 21.09' perl -e 'print "I can'\''t add" unless 19.08 + 2.01 == 21.09'
      BTW as Re: Strange (rounding?) problem points out, Perl often handles floats better if you use eq instead of == for testing equality...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found