"be consistent" PerlMonks

### Re: 02.5 == 25 - WTF?

by syphilis (Bishop)
 on Nov 30, 2020 at 23:11 UTC Need Help??

in reply to 02.5 == 25 - WTF?

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

Replies are listed 'Best First'.
Re^2: 02.5 == 25 - WTF?
by LanX (Cardinal) on Dec 01, 2020 at 00:35 UTC
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
Re^2: 02.5 == 25 - WTF?
by Eily (Monsignor) on Nov 30, 2020 at 23:21 UTC

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
Re^2: 02.5 == 25 - WTF?
by GrandFather (Saint) on Nov 30, 2020 at 23:48 UTC

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
Re^2: 02.5 == 25 - WTF?
by GrandFather (Saint) on Dec 01, 2020 at 03:39 UTC

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

Create A New User
Node Status?
node history
Node Type: note [id://11124424]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2021-05-13 19:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Perl 7 will be out ...

Results (142 votes). Check out past polls.

Notices?