Skip to content

Commit

Permalink
Missing Number properties
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
toastal committed Oct 19, 2021
1 parent f5bbd96 commit 58f2c26
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
8 changes: 8 additions & 0 deletions src/Data/Number.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -17,3 +21,7 @@ exports.fromStringImpl = function(str, isFinite, just, nothing) {
return nothing;
}
};

exports.maxValue = Number.MAX_VALUE;

exports.minValue = Number.MIN_VALUE;
23 changes: 22 additions & 1 deletion src/Data/Number.purs
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

0 comments on commit 58f2c26

Please sign in to comment.