Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

02.5 == 25 - WTF?

by GrandFather (Saint)
on Nov 30, 2020 at 22:52 UTC ( [id://11124421]=perlquestion: print w/replies, xml ) Need Help??

GrandFather has asked for the wisdom of the Perl Monks concerning the following question:

print 02.5, "\n"; print 02.9, "\n"; print 025, "\n";

Prints:

25 29 21

Which I found somewhat surprising! Should I be surprised?

Yes, I know the leading 0 digit denotes an octal value. But that doesn't account for 02.5 == 25 or 02.9 == 29 does it?

I was somewhat fortunate. This unexpected result could have been somewhat louder than it actually was. 25 Amps is not at all the same thing as 2.5 Amps! Fortunately the current was limited to 5 Amps!

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re: 02.5 == 25 - WTF?
by syphilis (Archbishop) on Nov 30, 2020 at 23:11 UTC
    But that doesn't account for 02.5 == 25 or 02.9 == 29 does it?

    Aaah ... but it's not claiming that.
    It's claiming that 02 . 5 eq '25' and 02 . 9 eq '29'.
    I think it's just that perl doesn't do octal fractional values.
    So the "." is considered to be the concatenation operator rather than the radix point.

    As to whether you should be surprised ... I'm inclined to say "yes" ... but I haven't given it a lot of thought.

    UPDATE: Perl does the same thing with hex values:
    C:\>perl -le "print 0x2.5;" 25
    Cheers,
    Rob
      LOL, B::Deparse is normally the best way to show such precedence problems, but constant folding is hiding it! :)

      D:\>perl -MO=Deparse -E"say 02.5" use feature 'current_sub', 'evalbytes', 'fc', 'postderef_qq', 'say', ' +state', 'switch', 'unicode_strings', 'unicode_eval'; say '25'; -e syntax OK

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        With hindsight, the fact that 25 is a quoted string is a hint.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      Both operators work though.

      say '==' if 02.5 == 25; say 'eq' if 02.5 eq 25;
      prints both == and eq (not that it's surprising either, given perl's seemless conversion between strings and numbers)

        prints both == and eq

        Yes - though making assumptions about equivalence based on the output of perl's print() function is not a good practice.
        For example:
        C:\>perl -le "print sqrt(2);" 1.4142135623731 C:\>perl -le "print 'WTF' unless sqrt(2) == 1.4142135623731;" WTF
        Cheers,
        Rob

      Sigh! I use . to concatenate strings so infrequently that I just didn't think of it in that role. :-(

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      Using Perl to drive moderately high power electronics (400W) does give "smoke testing" a whole other meaning - perhaps closer the the original meaning?

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        It's probably overkill, but I'll give it a plug, anyway:
        C:\>perl -MMath::MPFR -le "print Math::MPFR->new('2.5', 8);" 2.625
        Math::GMPf is another perl module that can convert octal floating point values to decimal.
        Are there any other modules doing that ? (I would expect so, but I haven't checked.)
        I couldn't spot any built-in perl functions that will readily do this.

        Cheers,
        Rob
Re: 02.5 == 25 - WTF?
by Eily (Monsignor) on Nov 30, 2020 at 23:13 UTC

    I expect that trying print 010.5, "\n", which prints 85 might be a clue about what's happening. It seems like octal representation is always interpreted as an int, so you have int(010).5. In which case the dot is the concatenation operator.

Re: 02.5 == 25 - WTF?
by tybalt89 (Monsignor) on Dec 01, 2020 at 02:08 UTC

    You were just unlucky you didn't try

    print 09.5, "\n";

    which would have made the problem clearer.

Re: 02.5 == 25 - WTF?
by Anonymous Monk on Nov 30, 2020 at 23:58 UTC
Re: 02.5 == 25 - WTF?
by stevieb (Canon) on Dec 01, 2020 at 15:36 UTC
    "25 Amps is not at all the same thing as 2.5 Amps!"

    I have definitely and literally found out the hard way a couple of times that unexpected results can do real damage when dealing with real electricity :)

      real damage when dealing with real electricity

      "Toy" electricity is already sufficient for real damage, see Re: I came, I saw, I .... But yes, "real" electricity has more interesting results and significantly more magic smoke escaping. See this story from wired.com and this embedded video of a 27 ton generator connected to the power grid out of phase.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      What!! You mean reality is not a video game with a Restart Button ? Yikes!

Re: 02.5 == 25 - WTF?
by syphilis (Archbishop) on Mar 29, 2021 at 19:29 UTC
    print 02.5, "\n"; print 02.9, "\n"; print 025, "\n";


    I've just realized that the following will do what you probably expect:
    C:\>perl -wle "print 02.5p+0;" 2.625 C:\>perl -wle "print 02.9p+0" Bareword found where operator expected at -e line 1, near "9p" (Missing operator before p?) Unquoted string "p" may clash with future reserved word at -e line 1. syntax error at -e line 1, near "9p" Execution of -e aborted due to compilation errors. C:\>perl -wle "print 025p+0" 21
    Cheers,
    Rob

Log In?
Username:
Password:

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

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

    No recent polls found