diff --git a/lib/fn.js b/lib/fn.js index 2d67f8e74..2b98650ba 100644 --- a/lib/fn.js +++ b/lib/fn.js @@ -20,13 +20,17 @@ Fn.toFixed = function(number, digits) { // Re-maps a number from one range to another. // Based on arduino map() // -// Return float +// Return Float32 // +var f32A = new Float32Array(1); + Fn.fmap = function(value, fromLow, fromHigh, toLow, toHigh) { - return (value - fromLow) * (toHigh - toLow) / - (fromHigh - fromLow) + toLow; + f32A[0] = (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow; + return f32A[0]; }; + + // Alias Fn.fscale = Fn.fmap; @@ -37,8 +41,11 @@ Fn.fscale = Fn.fmap; // // Retun int // +var s32A = new Int32Array(1); + Fn.map = function(value, fromLow, fromHigh, toLow, toHigh) { - return Fn.fmap(value, fromLow, fromHigh, toLow, toHigh) | 0; + s32A[0] = (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow; + return s32A[0]; }; // Alias diff --git a/test/fn.js b/test/fn.js index 4966d666e..081ba76ea 100644 --- a/test/fn.js +++ b/test/fn.js @@ -9,19 +9,16 @@ exports["Fn"] = { map: function(test) { test.expect(3); - test.equal(Fn.map(1009, 300, 1009, 0, 255), 255); test.equal(Fn.map(300, 300, 1009, 0, 255), 0); test.equal(Fn.map(500, 0, 1000, 0, 255), 127); - test.done(); }, fmap: function(test) { - test.expect(1); - + test.expect(2); test.equal(Fn.fmap(500, 0, 1000, 0, 255), 127.5); - + test.equal(Fn.fmap(512, 0, 1023, 0, 255), 127.6246337890625); test.done(); },