Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Number properties #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (#19 by @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