Thankyou everyone for your responses.
I have looked at the various Math packages and will consider using them where appropriate.
The question was more directed to the behaviour of the apparent concatenation and in the simplest case how best to treat a string that is formatted as such.
It would appear sprintf to remove any leading 0's seems like the safest bet prior to attempting to perform any basic math.
Thanks again!
| [reply] |
The question was more directed to the behaviour of the apparent concatenation and in the simplest case how best to treat a string that is formatted as such.
As I explained above, if the leading-zeros value is read from a file, the keyboard, a DB, or any other source, you do not have to do anything, Perl will treat them exactly as you want them to be treated.
The only time this arises is if you type leading zeros on values in your source code. So don't do that.
That's not to say that don't I wish it were not the case that leading-zero numbers were taken to be octal; or that I don't wish that it could be changed. But it can't and won't, so there's no point wishing for it. :(
But in the big picture, omitting leading zeros in source code is not so hard.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] |
No!
If you are assigning a decimal floating point numeric value to a variable in the code, just put it in there like you would do it on a calculator.
On the calculator keypad, to multiply 0000.7 x 4, I don't put in 0000.7 x 4 or even 00.7 x 4 - I would put just .7 x 4 or at most 0.7 x 4 and be done with it. That works for Perl variable assignment.
Extra leading zero's past what is required messes thing up!
But that only happens in an explicit assignment in the actual code - not when a variable is read from a file.
This octal stuff is legacy stuff, the halt instruction on one computer I worked was 063077 (octal) - as near I as remember - octal math is very rare nowadays, but the legacy exists and will continue to exist (e.g. Unix permission masks). It normally has no place in Perl math assignments.
If you make an explicit assignment like $x =0.7876543, don't code it as $x =00.7876543 No special care is needed when reading from a file.
| [reply] |
>perl -wMstrict -le
"for my $s (qw(0000.76 000.76 00.76 0.76 .76)) {
die qq{oops - '$s' * 4 != 3.04} if $s * 4 != 3.04;
}
print 'aok';
"
aok
| [reply] [d/l] [select] |
Thanks this makes sense now! A trap for the unwary, but never-the-less logical and consistent. | [reply] |