From 58f2c26a8291b9f755642455fd838b28cadd1b20 Mon Sep 17 00:00:00 2001 From: toastal Date: Tue, 19 Oct 2021 12:42:01 +0700 Subject: [PATCH] Missing `Number` properties MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No pun intended, a number of properties are missing from JavaScript’s `Number` as seen here: https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.epsilon This includes: `EPSILON`, `NEGATIVE_INFINITY`, `MAX_VALUE`, and `MIN_VALUE`. I have a current use case for `MAX_VALUE`. --- CHANGELOG.md | 1 + src/Data/Number.js | 8 ++++++++ src/Data/Number.purs | 23 ++++++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d51397a..06d64e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: +- Added `epsilon`, `negativeInfinity`, `maxValue`, and `minValue` from JavaScript’s `Number` properties (@toastal) Bugfixes: diff --git a/src/Data/Number.js b/src/Data/Number.js index 16cbe4e..e03fe29 100644 --- a/src/Data/Number.js +++ b/src/Data/Number.js @@ -1,12 +1,16 @@ /* globals exports */ "use strict"; +exports.epsilon = Number.EPSILON; + exports.nan = NaN; exports.isNaN = isNaN; exports.infinity = Infinity; +exports.negativeInfinity = Number.NEGATIVE_INFINITY; + exports.isFinite = isFinite; exports.fromStringImpl = function(str, isFinite, just, nothing) { @@ -17,3 +21,7 @@ exports.fromStringImpl = function(str, isFinite, just, nothing) { return nothing; } }; + +exports.maxValue = Number.MAX_VALUE; + +exports.minValue = Number.MIN_VALUE; diff --git a/src/Data/Number.purs b/src/Data/Number.purs index 3c6a587..1a34f04 100644 --- a/src/Data/Number.purs +++ b/src/Data/Number.purs @@ -1,24 +1,37 @@ -- | Functions for working with PureScripts builtin `Number` type. module Data.Number ( fromString + , epsilon , nan , isNaN , infinity + , negativeInfinity , isFinite + , minValue + , maxValue ) where import Data.Function.Uncurried (Fn4, runFn4) import Data.Maybe (Maybe(..)) +-- | The `Number` value for the magnitude of the difference between 1 and +-- | the smallest value greater than 1 that is representable as a +-- | `Number` value, which is approximately +-- | 2.2204460492503130808472633361816 × 10⁻¹⁶ +foreign import epsilon :: Number + -- | Not a number (NaN) foreign import nan :: Number -- | Test whether a number is NaN foreign import isNaN :: Number -> Boolean --- | Positive infinity +-- | Positive infinity, +∞𝔽 foreign import infinity :: Number +-- | Negative inifinity, -∞𝔽 +foreign import negativeInfinity :: Number + -- | Test whether a number is finite foreign import isFinite :: Number -> Boolean @@ -53,3 +66,11 @@ fromString :: String -> Maybe Number fromString str = runFn4 fromStringImpl str isFinite Just Nothing foreign import fromStringImpl :: Fn4 String (Number -> Boolean) (forall a. a -> Maybe a) (forall a. Maybe a) (Maybe Number) + +-- | The largest positive finite value of the `Number` type, which is +-- | approximately 1.7976931348623157 × 10³⁰⁸ +foreign import maxValue :: Number + +-- | The smallest positive value of the `Number` type, which is +-- | approximately 5 × 10⁻³²⁴. +foreign import minValue :: Number