How do you ++ a fraction?
by fglock (Vicar) on Aug 02, 2002 at 13:30 UTC
|
How do you "add 1" to last digit of a fraction such that
0.0012 gives 0.0013
0.99999 gives 1
Update: rephrased question
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
sub incdec {
$number = shift;
# do something if not /^\d*\.\d+$/
return $number+1 if $number =~ /^\d+$/;
$number =~ /\.(\d+)$/;
return $number + ("1E-".length($1);
}
for $n (1, .004, 1.99, 1.79, 1.009999) {
print "$n++ = ".incdec($n)."\n";
}
gives:
1++ = 2
0.004++ = 0.005
1.99++ = 2
1.79++ = 1.8
1.009999++ = 1.01
Update: use E notation
--
Steve Marvell | [reply] [Watch: Dir/Any] [d/l] [select] |
|
$frac = shift;
$frac =~ s/(\d+).$/$1/; # remove last digit & get fraction size
$frac += "1E-" . length($1); # add 1E-(size) => 0.0001
print $frac;
Works for "0.123999", "10.12399" and ".12399"
The result is weird when the number does not have
a fractional part, but that is not my case.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
(1++) ++ = 3
(0.004++) ++ = 0.006
But...
(1.99++) ++ = 3
(1.79++) ++ = 1.9
(1.009999++) ++ = 1.02
--
Tommy
Too stupid to live.
Too stubborn to die.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Re: Round a fraction
by frankus (Priest) on Aug 02, 2002 at 14:45 UTC
|
Okay, I am definitely missing something here...
If not, why not just use sprintf
like perl -e 'printf("%0.5f",1.233394494955);
So what did I miss?
--
Brother Frankus.
¤
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
I want to round a number, keeping as many
digits as possible, if the number ends in /999.$/ .
So I'd need something like a "%0.(max)f" format
(maybe it exists?)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Round a fraction
by talexb (Chancellor) on Aug 02, 2002 at 12:58 UTC
|
In the regexp I'd just look for
- an arbitrarily long string of zeroes and chop them off starting with the first one and
- an arbitrarily long strong of nines and chop them off starting with the first one, then add one to the end of the remaining number to make it round up.
By handling rounding in both directions you save yourself some trouble later on.
You didn't say, was it necessary to retain the tiny amount that you adjusted by? My suggestion doesn't keep that information -- I assumed it was irrelevant.
--t. alex
"Mud, mud, glorious mud. Nothing quite like it for cooling the blood!"
--Michael Flanders and Donald Swann
| [reply] [Watch: Dir/Any] |
|
was it necessary to retain the tiny amount that you adjusted by?
No, that is just some quick hack.
add one to the end
Is there an easy way to do that? (besides using substr and x)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Round a fraction
by mojotoad (Monsignor) on Aug 02, 2002 at 20:04 UTC
|
I'm curious as to why you want to do this? If you're doing any sort of real calculations with the numbers (or to calculate them) then you should have some sort of idea of the number of significant figures you're dealing with from the original source.
Otherwise, I'm guessing this is just for aesthetics of presentation? I guess I'm just too steeped in scientific ambivalence when it comes to the beauty of numbers. I'd just pick either the number of sig figs you want to use, or the number of decimal places, and use sprintf.
This is not criticism -- I am curious as to what you want to do with your numbers.
Matt
| [reply] [Watch: Dir/Any] [d/l] |
Re: Round a fraction
by thelenm (Vicar) on Aug 02, 2002 at 15:49 UTC
|
The Math::BigFloat module may be of some use to you.
-- Mike
--
just,my${.02} | [reply] [Watch: Dir/Any] |
Re: Round a fraction
by fglock (Vicar) on Aug 02, 2002 at 17:10 UTC
|
my $frac = shift;
if ($frac =~ /\..{5,}000.$/) {
$frac =~ s/.$//;
}
elsif ($frac =~ /(\..{5,}999)(.)$/) {
$frac = $frac + ( (10-$2) . "E-" . length($1));
}
print "rounded to $frac";
note: $frac can be an integer; $frac must be positive
| [reply] [Watch: Dir/Any] [d/l] |