Skip to content

Commit 2d1a223

Browse files
JamieBallingallhdgarroodJordanMartinez
authored
Math migration (#18)
* Add constants that currently live in purescript-math * Add core math functions that currently live in purescript-math. Also export constants * Remove dependency on purescript-math * Update CHANGELOG * Update documentation in README.md and a little in Number.purs * Add tests for math functions * Fix warning and remove extraneous comment in test/Main.purs * Fix comment to remainder Co-authored-by: Harry Garrood <harry@garrood.me> * Fix comment to sqrt1_2 Co-authored-by: Harry Garrood <harry@garrood.me> * Rework tests in terms of eqAbsolute * Move examples from README.md to Data.Number * Update trig examples to use pi * Convert trunc from Purescript to Javascript with polyfill, similar to sign * Update exp and log docs Co-authored-by: Harry Garrood <harry@garrood.me> Co-authored-by: Jordan Martinez <jordanalex.martinez@protonmail.com>
1 parent 16acdf3 commit 2d1a223

File tree

7 files changed

+607
-58
lines changed

7 files changed

+607
-58
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Ported various functions & constants from `purescript-math` (#18 by @JamieBallingall)
11+
12+
Specifically...
13+
- `abs`, `sign`
14+
- `max`, `min` (which work differently than `Number`'s `Ord` instance)
15+
- `ceil`, `floor`, `trunc`, `remainder`/`%`, `round`
16+
- `log`
17+
- `exp`, `pow`, `sqrt`
18+
- `acos`, `asin`, `atan`, `atan2`, `cos`, `sin`, `tan`
19+
- Numeric constants: `e`, `ln2`, `ln10`, `log10e`, `log2e`, `pi`, `sqrt1_2`,
20+
`sqrt2`, and `tau`
1021

1122
Bugfixes:
1223

1324
Other improvements:
25+
- Removed dependency on `purescript-math`
1426

1527
## [v8.0.0](https://github.com/purescript/purescript-numbers/releases/tag/v7.0.0) - 2021-02-26
1628

README.md

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,23 @@ Utility functions for working with PureScripts builtin `Number` type.
1212
spago install numbers
1313
```
1414

15-
## Examples
16-
17-
Parsing:
18-
19-
```purs
20-
> fromString "12.34"
21-
(Just 12.34)
22-
23-
> fromString "1e-3"
24-
(Just 0.001)
25-
```
26-
27-
Formatting (`Data.Number.Format`):
28-
29-
```purs
30-
> let x = 1234.56789
31-
32-
> toStringWith (precision 6) x
33-
"1234.57"
34-
35-
> toStringWith (fixed 3) x
36-
"1234.568"
37-
38-
> toStringWith (exponential 2) x
39-
"1.23e+3"
40-
```
41-
42-
Approximate comparisons (`Data.Number.Approximate`):
43-
44-
```purs
45-
> 0.1 + 0.2 == 0.3
46-
false
47-
48-
> 0.1 + 0.2 ≅ 0.3
49-
true
50-
```
51-
52-
_NaN_ and _infinity_:
53-
54-
```purs
55-
> isNaN (Math.asin 2.0)
56-
true
57-
58-
> isFinite (1.0 / 0.0)
59-
false
60-
```
15+
## Scope
16+
17+
* Parsing with `fromString`
18+
* Formating with `toStringWith`, see `Data.Number.Format`
19+
* Approximate comparisions with ``, see `Data.Number.Approximate`
20+
* Not-a-number and infinite value detection with `isNaN` and `isFinite`
21+
* Remainder with `%`
22+
* Trignometric functions with `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, and
23+
`atan2`
24+
* Natural logarithm and exponents with `log` and `exp`
25+
* Powers with `sqrt` and `pow`
26+
* Rounding with `ceil`, `floor`, `round`, and `trunc`
27+
* Numeric minimum and maximum with `min` and `max`, which behave differently to
28+
the versions in `Data.Ord` on values of `NaN`
29+
* Sign and absolute value functions `sign` and `abs`
30+
* Numeric constants `e`, `ln 2`, `ln10`, `log10e`, `log2e`, `pi`, `sqrt1_2`,
31+
`sqrt2`, and `tau`
6132

6233
## Documentation
6334

bower.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
],
1818
"dependencies": {
1919
"purescript-functions": "master",
20-
"purescript-math": "master",
2120
"purescript-maybe": "master"
2221
},
2322
"devDependencies": {

src/Data/Number.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,67 @@ export function fromStringImpl(str, isFinite, just, nothing) {
1414
return nothing;
1515
}
1616
}
17+
18+
export const abs = Math.abs;
19+
20+
export const acos = Math.acos;
21+
22+
export const asin = Math.asin;
23+
24+
export const atan = Math.atan;
25+
26+
export const atan2 = function (y) {
27+
return function (x) {
28+
return Math.atan2(y, x);
29+
};
30+
};
31+
32+
export const ceil = Math.ceil;
33+
34+
export const cos = Math.cos;
35+
36+
export const exp = Math.exp;
37+
38+
export const floor = Math.floor;
39+
40+
export const log = Math.log;
41+
42+
export const max = function (n1) {
43+
return function (n2) {
44+
return Math.max(n1, n2);
45+
};
46+
};
47+
48+
export const min = function (n1) {
49+
return function (n2) {
50+
return Math.min(n1, n2);
51+
};
52+
};
53+
54+
export const pow = function (n) {
55+
return function (p) {
56+
return Math.pow(n, p);
57+
};
58+
};
59+
60+
export const remainder = function (n) {
61+
return function (m) {
62+
return n % m;
63+
};
64+
};
65+
66+
export const round = Math.round;
67+
68+
export const sign = Math.sign ? Math.sign : function(x) {
69+
return x === 0 || x !== x ? x : (x < 0 ? -1 : 1);
70+
};
71+
72+
export const sin = Math.sin;
73+
74+
export const sqrt = Math.sqrt;
75+
76+
export const tan = Math.tan;
77+
78+
export const trunc = Math.trunc ? Math.trunc : function(x) {
79+
return x < 0 ? Math.ceil(x) : Math.floor(x);
80+
}

0 commit comments

Comments
 (0)