# HG changeset patch # User Matthew Wild # Date 1379770282 -3600 # Node ID a689e0187ef55e796b41ff4387b221421d8492de # Parent 574e0baea1362654c32a7e4eb3e7dfedf709de76 Fix math.frexp() and add more tests (thanks Florob, Link Mauve, xnyhps) diff -r 574e0baea136 -r a689e0187ef5 lvm.js --- a/lvm.js Sat Sep 21 14:02:16 2013 +0100 +++ b/lvm.js Sat Sep 21 14:31:22 2013 +0100 @@ -850,10 +850,16 @@ { var m = x.value; var e = 0; - while (Math.abs(m) >= 1) { - m = m / 2; - e = e + 1; - }; + if(m != 0) { + while (Math.abs(m) < 0.5) { + m = m * 2; + e = e - 1; + } + while (Math.abs(m) >= 1) { + m = m / 2; + e = e + 1; + } + } return [this.LValue(m), this.LValue(e)]; }, floor: function (x) diff -r 574e0baea136 -r a689e0187ef5 tests/pass/frexp.lua --- a/tests/pass/frexp.lua Sat Sep 21 14:02:16 2013 +0100 +++ b/tests/pass/frexp.lua Sat Sep 21 14:31:22 2013 +0100 @@ -8,4 +8,10 @@ assert(m == 0.75 and e == 2, "frexp(3)"); local m, e = math.frexp(-1); -assert(m == -0.5 and e == 1, "frexp(1)"); +assert(m == -0.5 and e == 1, "frexp(-1)"); + +local m, e = math.frexp(0.5); +assert(m == 0.5 and e == 0, "frexp(0.5)"); + +local m, e = math.frexp(0.1); +assert(m == 0.8 and e == -3, "frexp(0.1)");