Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^5: Calculating base difference of numbers

by ikegami (Patriarch)
on Jun 01, 2008 at 03:01 UTC ( [id://689491]=note: print w/replies, xml ) Need Help??


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

So you want .0001 of the magnitude of some number. That wasn't specified and different than what you agreed to.

Well, you have to start by finding the magnitude of the number. The amount by which you'll multiply is based on the magnitude.

use strict; use warnings; sub magnitude { my ($n, $p) = @_; $p ||= 16; # Num of significant digits. 16 max for doubles return 0 + ( sprintf('%.*e', $p-1, $n) =~ /e(.*)/ )[0]; } sub movement { my ($old, $new) = @_; my $m = magnitude($old); return 0 + sprintf '%.0f', ($new-$old) * 10**(4-$m); } printf("%+.0f\n", movement(@ARGV));
>perl movement.pl 1.0025 1.003 +5 >perl movement.pl 10.025 10.03 +5

You haven't defined what you want to happen when the number are of different magnitude.

>perl movement.pl 98000 99000 +1000 >perl movement.pl 99000 100000 +1000 >perl movement.pl 100000 99000 -100 >perl movement.pl 99000 98000 -1000

Another edge case is an input of zero. You haven't defined when you want to happen in that circumstance either.

Log In?
Username:
Password:

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

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

    No recent polls found