in reply to Re^2: Perl with precision
in thread Perl with precision

See Re^2: Perl with precision (octal). 000.76 is interpreted as 000 . 76 where 000 is treated as octal and becomes just 0.

Concatenation makes both sides strings so you end up with '0' . '76', which when multiplied becomes numeric as just 76 ...


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".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^4: Perl with precision
by AlienSpaces (Initiate) on Feb 29, 2012 at 02:01 UTC
    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!
      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      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.

      apparent concatenation and ... how best to treat a string that is formatted as such.

      I don't understand your concern vis-a-vis strings. As noted in other replys, there should be no problem with any such string without resort to sprintf. (But always beware F.P. representation approximation!) Can you cite an example of a string-related problem?

      >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
Re^4: Perl with precision
by Marshall (Canon) on Feb 29, 2012 at 00:56 UTC
    Thanks this makes sense now! A trap for the unwary, but never-the-less logical and consistent.