From 5e64716e82d36e3d0a79a07c3e6053bc608599cd Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 25 Mar 2022 09:14:59 -0500 Subject: [PATCH 1/8] Migrate trunc from math package --- src/Data/Int.js | 4 ++++ src/Data/Int.purs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/Data/Int.js b/src/Data/Int.js index 9829cce..3178ada 100644 --- a/src/Data/Int.js +++ b/src/Data/Int.js @@ -63,3 +63,7 @@ export const pow = function (x) { return Math.pow(x,y) | 0; }; }; + +export const trunc = Math.trunc || function (n) { + return n < 0 ? Math.ceil(n) : Math.floor(n); +}; diff --git a/src/Data/Int.purs b/src/Data/Int.purs index 28952ed..469ba55 100644 --- a/src/Data/Int.purs +++ b/src/Data/Int.purs @@ -21,6 +21,7 @@ module Data.Int , quot , rem , pow + , trunc ) where import Prelude @@ -241,6 +242,10 @@ foreign import rem :: Int -> Int -> Int -- | Raise an Int to the power of another Int. foreign import pow :: Int -> Int -> Int +-- | 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 -> Int + foreign import fromStringAsImpl :: (forall a. a -> Maybe a) -> (forall a. Maybe a) From 5683a374662b19c0f292b09691f591d72b404ea1 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 25 Mar 2022 09:15:58 -0500 Subject: [PATCH 2/8] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ddf194..7e9e941 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: - Migrate FFI to ES modules (#50 by @kl0tl and @JordanMartinez) +- Migrate `trunc` from `math` package (#51 by @JordanMartinez) New features: From f7dad9416b6ae3e2c7dcc4a72fa0ccd49230669e Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 25 Mar 2022 09:18:22 -0500 Subject: [PATCH 3/8] Use Number's trunc; update docs --- src/Data/Int.purs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Data/Int.purs b/src/Data/Int.purs index 469ba55..1e1b639 100644 --- a/src/Data/Int.purs +++ b/src/Data/Int.purs @@ -242,9 +242,11 @@ foreign import rem :: Int -> Int -> Int -- | Raise an Int to the power of another Int. foreign import pow :: Int -> Int -> Int --- | 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 -> Int +-- | Convert a `Number` to an `Int`, by dropping the decimal. +-- | Values outside the `Int` range are clamped, `NaN` and `Infinity` +-- | values return 0. +trunc :: Number -> Int +trunc = unsafeClamp <<< Number.trunc foreign import fromStringAsImpl :: (forall a. a -> Maybe a) From d1e517140caf06c2efdfbd3c8f085ff6a1777d16 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 25 Mar 2022 09:19:07 -0500 Subject: [PATCH 4/8] Reposition trunc to be closer to floor --- src/Data/Int.purs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Data/Int.purs b/src/Data/Int.purs index 1e1b639..b8ebf55 100644 --- a/src/Data/Int.purs +++ b/src/Data/Int.purs @@ -2,6 +2,7 @@ module Data.Int ( fromNumber , ceil , floor + , trunc , round , toNumber , fromString @@ -21,7 +22,6 @@ module Data.Int , quot , rem , pow - , trunc ) where import Prelude @@ -56,6 +56,12 @@ floor = unsafeClamp <<< Math.floor ceil :: Number -> Int ceil = unsafeClamp <<< Math.ceil +-- | Convert a `Number` to an `Int`, by dropping the decimal. +-- | Values outside the `Int` range are clamped, `NaN` and `Infinity` +-- | values return 0. +trunc :: Number -> Int +trunc = unsafeClamp <<< Number.trunc + -- | Convert a `Number` to an `Int`, by taking the nearest integer to the -- | argument. Values outside the `Int` range are clamped, `NaN` and `Infinity` -- | values return 0. @@ -242,12 +248,6 @@ foreign import rem :: Int -> Int -> Int -- | Raise an Int to the power of another Int. foreign import pow :: Int -> Int -> Int --- | Convert a `Number` to an `Int`, by dropping the decimal. --- | Values outside the `Int` range are clamped, `NaN` and `Infinity` --- | values return 0. -trunc :: Number -> Int -trunc = unsafeClamp <<< Number.trunc - foreign import fromStringAsImpl :: (forall a. a -> Maybe a) -> (forall a. Maybe a) From 3a1ac72ce07632e59cd7b82f9dd0c4149fe224f6 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 25 Mar 2022 09:19:14 -0500 Subject: [PATCH 5/8] Drop FFI for trunc --- src/Data/Int.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Data/Int.js b/src/Data/Int.js index 3178ada..9829cce 100644 --- a/src/Data/Int.js +++ b/src/Data/Int.js @@ -63,7 +63,3 @@ export const pow = function (x) { return Math.pow(x,y) | 0; }; }; - -export const trunc = Math.trunc || function (n) { - return n < 0 ? Math.ceil(n) : Math.floor(n); -}; From 65a59ee4a04f412fc67951288800d087fee648f9 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 25 Mar 2022 16:27:59 -0500 Subject: [PATCH 6/8] Update code to use fns from numbers --- src/Data/Int.purs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Data/Int.purs b/src/Data/Int.purs index b8ebf55..c637fc8 100644 --- a/src/Data/Int.purs +++ b/src/Data/Int.purs @@ -29,8 +29,7 @@ import Prelude import Data.Int.Bits ((.&.)) import Data.Maybe (Maybe(..), fromMaybe) import Data.Number (isFinite) - -import Math as Math +import Data.Number as Number -- | Creates an `Int` from a `Number` value. The number must already be an -- | integer and fall within the valid range of values for the `Int` type @@ -48,13 +47,13 @@ foreign import fromNumberImpl -- | less than the argument. Values outside the `Int` range are clamped, `NaN` -- | and `Infinity` values return 0. floor :: Number -> Int -floor = unsafeClamp <<< Math.floor +floor = unsafeClamp <<< Number.floor -- | Convert a `Number` to an `Int`, by taking the closest integer equal to or -- | greater than the argument. Values outside the `Int` range are clamped, -- | `NaN` and `Infinity` values return 0. ceil :: Number -> Int -ceil = unsafeClamp <<< Math.ceil +ceil = unsafeClamp <<< Number.ceil -- | Convert a `Number` to an `Int`, by dropping the decimal. -- | Values outside the `Int` range are clamped, `NaN` and `Infinity` @@ -66,7 +65,7 @@ trunc = unsafeClamp <<< Number.trunc -- | argument. Values outside the `Int` range are clamped, `NaN` and `Infinity` -- | values return 0. round :: Number -> Int -round = unsafeClamp <<< Math.round +round = unsafeClamp <<< Number.round -- | Convert an integral `Number` to an `Int`, by clamping to the `Int` range. -- | This function will return 0 if the input is `NaN` or an `Infinity`. From 8e60549d0dc4a0d8e434acc877628d7dadc0d48f Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 25 Mar 2022 16:28:53 -0500 Subject: [PATCH 7/8] Drop dependency on math; update changelog --- CHANGELOG.md | 1 + bower.json | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e9e941..3cea7e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ New features: Bugfixes: Other improvements: +- Drop dependency on deprecated `math` package (#51 by @JordanMartinez) ## [v5.0.0](https://github.com/purescript/purescript-integers/releases/tag/v5.0.0) - 2021-02-26 diff --git a/bower.json b/bower.json index 154ef73..7cea11a 100644 --- a/bower.json +++ b/bower.json @@ -16,7 +16,6 @@ "package.json" ], "dependencies": { - "purescript-math": "master", "purescript-maybe": "master", "purescript-numbers": "master", "purescript-prelude": "master" From c825f35daa4d16bc78538029d7925bb26866cb76 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 25 Mar 2022 16:30:01 -0500 Subject: [PATCH 8/8] Fix compiler warning in tests --- test/Test/Data/Int.purs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Test/Data/Int.purs b/test/Test/Data/Int.purs index ae3df7a..1eabc6b 100644 --- a/test/Test/Data/Int.purs +++ b/test/Test/Data/Int.purs @@ -155,7 +155,6 @@ testInt = do let q = quot a b r = rem a b - msg = show a <> " / " <> show b <> ": " in do assert $ q * b + r == a -- Check when dividend goes into divisor exactly