Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Calculating base difference of numbers

by tachyon-II (Chaplain)
on May 31, 2008 at 00:44 UTC ( [id://689371]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Calculating base difference of numbers
in thread Calculating base difference of numbers

Sadly this will fail for any movement of greater than 9 points. This problem is insoluble without a definition of what constitutes a point. Consider the upper number pair and some possible variations that could exist with trailing zero truncation:

1.5553 - 1.5552 # 1 point 1.5553 - 1.55 # 53 points (your sub will fail with this) 1.56 - 1.55 # 1 point? 100 points? (insoluble from data alone)

You either have to decide what is a point, or guess and accept that there will be errors:

sub points { my ($was,$is,$exp) = @_; unless ($exp) { $was =~ m/\.(\d+)$/; my $x = $1 ? length($1) : 0; $is =~ m/\.(\d+)$/; my $y = $1 ? length($1) : 0; $exp = $x > $y ? $x : $y; } print "($exp) $was => $is\t"; $exp = 10**$exp; my $dif = $is*$exp - $was*$exp; # add correction factor to allow int to round correctly # also ensures that FP "error" such as 4.999999 ends up as +5 $dif += $dif < 0 ? -0.5 : +0.5; return sprintf "%+d", $dif; } print points(1.5553,1.5552), $/; print points(1.55,1.5553 ), $/; print points(1.55,1.56), $/; print points(1.55,1.56,4), $/; print points(0.9984,0.998), $/; __DATA__ (4) 1.5553 => 1.5552 -1 (4) 1.55 => 1.5553 +53 (2) 1.55 => 1.56 +1 (4) 1.55 => 1.56 +100 (4) 0.9984 => 0.998 -4

Log In?
Username:
Password:

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

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

    No recent polls found