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 | [reply] [d/l] [select] |
|
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
| [reply] [d/l] |
|
| [reply] |
|
|
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) | [reply] [d/l] [select] |
|
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 | [reply] [d/l] |
|
| [reply] |
|
| [reply] |
|
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 | [reply] [d/l] |
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.
| [reply] [d/l] [select] |
Re: 02.5 == 25 - WTF?
by tybalt89 (Monsignor) on Dec 01, 2020 at 02:08 UTC
|
print 09.5, "\n";
which would have made the problem clearer.
| [reply] [d/l] |
Re: 02.5 == 25 - WTF?
by Anonymous Monk on Nov 30, 2020 at 23:58 UTC
|
$ ppi_dumper 2
PPI::Document
PPI::Statement
PPI::Token::Word 'print'
PPI::Token::Whitespace ' '
PPI::Token::Number::Octal '02'
PPI::Token::Number::Float '.5'
PPI::Token::Whitespace '\n'
:O you've found a bug :D an old one https://github.com/adamkennedy/PPI/issues/165 | [reply] [d/l] |
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 :)
| [reply] |
|
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". ;-)
| [reply] |
|
| [reply] |
Re: 02.5 == 25 - WTF?
by syphilis (Archbishop) on Mar 29, 2021 at 19:29 UTC
|
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 | [reply] [d/l] [select] |