From 08207af2634ca2950be837fb68a442969481b6d3 Mon Sep 17 00:00:00 2001 From: Ryan Rempel Date: Sun, 10 Apr 2016 09:57:22 -0500 Subject: [PATCH] Add `trunc`, with a polyfill for browsers that don't support it. --- src/Math.js | 4 ++++ src/Math.purs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/Math.js b/src/Math.js index 8aad982..e9a7af8 100644 --- a/src/Math.js +++ b/src/Math.js @@ -24,6 +24,10 @@ exports.exp = Math.exp; exports.floor = Math.floor; +exports.trunc = Math.trunc || function (n) { + return n < 0 ? Math.ceil(n) : Math.floor(n); +}; + exports.log = Math.log; exports.max = function (n1) { diff --git a/src/Math.purs b/src/Math.purs index 6f4b6cb..a9ba443 100644 --- a/src/Math.purs +++ b/src/Math.purs @@ -60,6 +60,10 @@ foreign import sqrt :: Number -> Number -- | Returns the tangent of the argument. foreign import tan :: Radians -> Number +-- | Truncates the decimal portion of a number. Equivalent to `floor` if the +-- | number is positive, and `ceil` if the number is negative. +foreign import trunc :: Number -> Number + infixl 7 % -- | Computes the remainder after division, wrapping Javascript's `%` operator.