diff options
-rw-r--r-- | Libraries/LibJS/Tests/Math.floor.js | 19 | ||||
-rw-r--r-- | Libraries/LibM/math.cpp | 10 |
2 files changed, 27 insertions, 2 deletions
diff --git a/Libraries/LibJS/Tests/Math.floor.js b/Libraries/LibJS/Tests/Math.floor.js new file mode 100644 index 0000000000..c636398969 --- /dev/null +++ b/Libraries/LibJS/Tests/Math.floor.js @@ -0,0 +1,19 @@ +load("test-common.js"); + +try { + assert(Math.floor(0.95) === 0); + assert(Math.floor(4) === 4); + assert(Math.floor(7.004) == 7); + assert(Math.floor(-0.95) === -1); + assert(Math.floor(-4) === -4); + assert(Math.floor(-7.004) === -8); + + assert(isNaN(Math.floor())); + assert(isNaN(Math.floor(NaN))); + + assert(Math.floor.length === 1); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibM/math.cpp b/Libraries/LibM/math.cpp index 38d9edec1d..021993e9d4 100644 --- a/Libraries/LibM/math.cpp +++ b/Libraries/LibM/math.cpp @@ -412,12 +412,18 @@ float roundf(float value) float floorf(float value) { - return (int)value; + if (value >= 0) + return (int)value; + int intvalue = (int)value; + return ((float)intvalue == value) ? intvalue : intvalue - 1; } double floor(double value) { - return (int)value; + if (value >= 0) + return (int)value; + int intvalue = (int)value; + return ((double)intvalue == value) ? intvalue : intvalue - 1; } double rint(double value) |