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".
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
So what you are saying is that the first part is octal zero and the dot is the concatenation operator?
| [reply] |
|
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
| [reply] [d/l] |
|
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".
| [reply] [d/l] [select] |
|
|
|
|
|
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.
| [reply] |
Re: Perl with precision
by Anonymous Monk on Feb 28, 2012 at 23:52 UTC
|
| [reply] |
|
Psst. Your replies are more relevant if you read more than just the title before replying.
| [reply] |
|
| [reply] |
|
|
|
|
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.)
| [reply] |