# HG changeset patch # User Matthew Wild # Date 1379768536 -3600 # Node ID 574e0baea1362654c32a7e4eb3e7dfedf709de76 # Parent 16b833862ae250cce02a37cd2c0ecf70f30af307 Add math.frexp() and tests (many thanks to Florob and Link Mauve!) diff -r 16b833862ae2 -r 574e0baea136 lvm.js --- a/lvm.js Wed Nov 24 03:36:14 2010 +0000 +++ b/lvm.js Sat Sep 21 14:02:16 2013 +0100 @@ -846,6 +846,16 @@ { return [this.LValue(m.value*Math.pow(2, e.value))]; }, + frexp: function (x) + { + var m = x.value; + var e = 0; + while (Math.abs(m) >= 1) { + m = m / 2; + e = e + 1; + }; + return [this.LValue(m), this.LValue(e)]; + }, floor: function (x) { return [this.LValue(Math.floor(x.value))]; diff -r 16b833862ae2 -r 574e0baea136 tests/pass/frexp.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pass/frexp.lua Sat Sep 21 14:02:16 2013 +0100 @@ -0,0 +1,11 @@ +local m, e = math.frexp(1); +assert(m == 0.5 and e == 1, "frexp(1)"); + +local m, e = math.frexp(0); +assert(m == 0 and e == 0, "frexp(0)"); + +local m, e = math.frexp(3); +assert(m == 0.75 and e == 2, "frexp(3)"); + +local m, e = math.frexp(-1); +assert(m == -0.5 and e == 1, "frexp(1)");