Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Perl with precision

by AlienSpaces (Initiate)
on Feb 28, 2012 at 23:31 UTC ( [id://956797]=perlquestion: print w/replies, xml ) Need Help??

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

my $a = 000.76; my $b = 4; my $c = $a * $b; print $c; 304 my $a = .76; my $b = 4; my $c = $a * $b; print $c; 3.04 my $a = 0.76; my $b = 4; my $c = $a * $b; print $c; 3.04 my $a = 0000.76; my $b = 4; my $c = $a * $b; print $c; 304 my $a = 0000.76 + 0; my $b = 4; my $c = $a * $b; print $c; 304 my $a = .76; my $b = 4; my $c = $a * $b; print $c; 3.04 my $a = "000.76"; my $b = 4; my $c = $a * $b; print $c; 3.04
Would love to know the best way to handle numbers with precision in Perl. These numbers (ie: strings) may come from anywhere such as a database, a spread sheet etc What is the best way to clean them, add them, multiply them with confidence? :) Thanks in advance.

Replies are listed 'Best First'.
Re: Perl with precision
by BrowserUk (Patriarch) on Feb 28, 2012 at 23:52 UTC
    These numbers (ie: strings) may come from anywhere such as a database, a spread sheet etc What is the best way to clean them, add them, multiply them with confidence? :)

    Your problem is artificial because you could never get values in the form you show other than by hardcoding them in the source code:

    C:\test>perl -E"my $a = 000.76; my $b = 4; my $c = $a * $b; say $c;" 304

    If they had come from an external source, then they would be actual strings, and be handled correctly:

    C:\test>perl -E"my $a = '000.76'; my $b = 4; my $c = $a * $b; say $c;" 3.04

    I'm not actually sure what is happening in the first case, though I suspect it has something to do with so-called "Version strings" (aka. v-strings) (maybe in combination with octal? And possibly a bug?), but the situation only arises with values hard-coded into the source, not when read from external sources.


    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?

      A start of 0[0-7] means "octal". So 000.76 is simply the same as 000 . 76 which is '076' which becomes just 76 when used as a number.

      - tye        

        So what you are saying is that the first part is octal zero and the dot is the concatenation operator?
      This is truly bizarre but I was able to confirm this on my installation.
      I'd never added extraneous leading zero'es in a constant before.
      #!/usr/bin/perl -w use strict; #tested on Active State 10.1 for Windows my $a = 000.76; #note: 0.76 works ok # "000.76" works ok # 00.76 fails # 000.76 fails my $b = 4; my $c = $a * $b; print "$c\n"; #304 not 3.04!! WHOA my $x = 000.76; print "$x\n"; #prints 076 $x = 000.76; $x +=0; print "$x\n"; #prints 76 my $y=0.76; print "$y\n"; #prints 0.76

        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?

Re: Perl with precision (strings!)
by tye (Sage) on Feb 28, 2012 at 23:46 UTC

    Only your last example has anything to do with your stated problem. And it works fine. So I don't think you'll have any problems.

    - tye        

Re: Perl with precision
by Anonymous Monk on Feb 28, 2012 at 23:52 UTC
      Psst. Your replies are more relevant if you read more than just the title before replying.

        Psst. Your replies are more relevant if you read more than just the title before replying.

        Can you explain how they are not relevant?

        It is not unreasonable to have assumed that the problem being posed actually did have to do with numeric precision as opposed to actually being (apparently) related to octal notation... If the issue had been something to do with high precision arithmetic, as the topic title certainly suggests, these links would have been very apropos. (And, to anyone who IS dealing with such a problem, they still are.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found