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.


In reply to Re^5: Calculating base difference of numbers by ikegami
in thread Calculating base difference of numbers by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.