Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Calculating base difference of numbers

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


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

Yes...and I feel stupid that I can't work it out...should be a simple mathematical equation.
  • Comment on Re^2: Calculating base difference of numbers

Replies are listed 'Best First'.
Re^3: Calculating base difference of numbers
by ikegami (Patriarch) on May 31, 2008 at 22:09 UTC
    my $diff = ($new*10000) - ($old*10000);
    Or if you want to round it,
    my $diff = 0 + sprintf '%.0f', ($new*10000) - ($old*10000);

    Update: The *10000 can be factored out: ($new-$old)*10000

      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

        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.

        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://689466]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found