Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math.scale should work for "negative" output scales too #7

Open
schnepe2 opened this issue Sep 29, 2016 · 0 comments
Open

Math.scale should work for "negative" output scales too #7

schnepe2 opened this issue Sep 29, 2016 · 0 comments

Comments

@schnepe2
Copy link

The current (as of 2016-09-29) implementation of Math.scale does not work if the output scale is "negative"

Math.scale(0.6, 0,1, 0,10);  // <= should return  6 [0...1] => [0...10]
Math.scale(0.6, 0,1, 10,0);  // <= should return  4 [0...1] => [10...0]
Math.scale(0.6, 0,1, 0,-10); // <= should return -6 [0...1] => [0...-10]

I also think this "flipping of scales" should maybe mentioned specifically in the reference / manual

A working polyfill seems to be this:

defineMath("scale", function scale(value, inMin, inMax, outMin, outMax)
{
  if ( arguments.length === 0 || Number.isNaN(value) ||
       Number.isNaN(inMin) || Number.isNaN(inMax) ||
       Number.isNaN(outMin) || Number.isNaN(outMax) )
  {
    return NaN;
  }

  if ( value === Infinity || value === -Infinity || (inMin == outMin && inMax == outMax) )
  {
    return value;
  }

  return outMin + ((value - inMin) / (inMax - inMin)) * (outMax - outMin);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant