Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: Calculating base difference of numbers

by Anonymous Monk
on May 31, 2008 at 23:44 UTC ( [id://689473]=note: print w/replies, xml ) Need Help??


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

If you run the math that doesn't work.

old => 100.25 new => 100.26

diff = (100.26 * 10000) - (100.25 * 10000)
= 100

when in fact the basis point movement is 1
  • Comment on Re^4: Calculating base difference of numbers

Replies are listed 'Best First'.
Re^5: Calculating base difference of numbers
by ikegami (Patriarch) on Jun 01, 2008 at 03:01 UTC

    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.

Re^5: Calculating base difference of numbers
by dragonchild (Archbishop) on Jun 01, 2008 at 00:16 UTC
    Actually, the basis point movement there is 100.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found