in reply to Re: Normalizing a range of numbers to a percentage
in thread Normalizing a range of numbers to a percentage

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?

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

    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)); # ^^^

        No.

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

        The offset on the new range has to be outside the multiply.