Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Normalizing a range of numbers to a percentage

by tybalt89 (Monsignor)
on Mar 07, 2019 at 23:15 UTC ( [id://1231035]=note: print w/replies, xml ) Need Help??


in reply to Normalizing a range of numbers to a percentage

my $x = (($tap - $min) * ($new_max - $new_min) / ($max - $min)) + $new +_min;

In your case $new_min was zero, so your numbers are good.

I think of it this way:

($tap - $min) / ($max - $min)
is the ratio on the old scale, then multiply by the new scale and add the new offset.

Replies are listed 'Best First'.
Re^2: Normalizing a range of numbers to a percentage
by stevieb (Canon) on Mar 07, 2019 at 23:19 UTC

    Thanks tybalt89!

    I did happen to simplify this down a bit because of the zero aspect, but for completeness purposes, may I bug you to add in a couple of examples as to where my OP code *wouldn't* work?

    Say if $new_min was 5 for example?

      With $new_min = 5, it would give a range of 0..95 rather than 5..100 if you don't include the offset.

      With -100 to +100 as the new min/max, it would give 0..200 rather than -100 to +100.

        "With $new_min = 5, it would give a range of 0..95 rather than 5..100 if you don't include the offset."

        So the following is what is meant here?

        my $x = (($tap - $min) * ($new_max - $new_min + 5) / ($max - $min)); # ^^^
Re^2: Normalizing a range of numbers to a percentage
by pryrt (Abbot) on Mar 07, 2019 at 23:16 UTC

    ugh, can't believe I missed that. I do that type of calculation so often, I should have noticed :-(

      I do it so often, I finally wrote a simultaneous equation solver (with Math::BigRat) so I just plug in end points and it gives me the multiplier and constant offset :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-19 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found