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

feat: add math module for high precision computing #375

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
60311ef
feat: add math/big module
axetroy May 2, 2019
fb54b09
refactor: revert change of tsconfig.json
axetroy May 2, 2019
c30b442
style: format the test file
axetroy May 2, 2019
480e645
feat: add copyright header for big module
axetroy May 3, 2019
1d9f0bb
refactor: add type for const
axetroy May 3, 2019
537509f
test: add more test case for big module
axetroy May 3, 2019
6ba47d1
test: add test case for big.abs()
axetroy May 3, 2019
d08f29d
test: add test case for big.cmp
axetroy May 3, 2019
c44c5b9
style: format the code
axetroy May 3, 2019
87e10db
test: add test case for big.mod()
axetroy May 3, 2019
091022a
test: add test case for big.pow()
axetroy May 3, 2019
ae8fdf3
test: add test case for big.round()
axetroy May 3, 2019
5b30dfc
test: add test case for big.sqrt()
axetroy May 3, 2019
7a4187c
test: update test case
axetroy May 3, 2019
0de73dc
test: add test case for big.toExponential()
axetroy May 3, 2019
545f09a
test: add test case for big.toFixed()
axetroy May 3, 2019
bfc4fba
test: add test case for big.toPrecision()
axetroy May 3, 2019
a5ada68
test: add test case to big.toString()
axetroy May 3, 2019
d8a514c
style: format the code
axetroy May 3, 2019
3b08ffe
test: update test case
axetroy May 3, 2019
8929dc1
feat: add common method for math module
axetroy May 3, 2019
bdcde65
feat: add sum() for math module
axetroy May 3, 2019
a583c58
fix: eslint warning
axetroy May 3, 2019
a48f962
feat: add abs for math module
axetroy May 3, 2019
65ec600
feat: add min() for math module
axetroy May 3, 2019
85fd9e6
feat: add max() for math module
axetroy May 3, 2019
0ae6185
docs: update vendor license
axetroy May 3, 2019
074e416
docs: add missing deno copyright header
axetroy May 3, 2019
58c73f7
docs: add jsdoc for math's methods
axetroy May 3, 2019
e32b6e6
docs: add math documenation
axetroy May 3, 2019
0d3d575
docs: add Deno license behind big.js license
axetroy May 3, 2019
c36ddbb
docs: update jsdoc comment
axetroy May 3, 2019
ebf6891
test: add test case for min
axetroy May 3, 2019
045bf3c
test: add test case for max()
axetroy May 3, 2019
741f925
test: add test case for sum()
axetroy May 3, 2019
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Here are the dedicated documentations of modules:
- [strings](strings/README.md)
- [testing](testing/README.md)
- [toml](toml/README.md)
- [math](math/README.md)

## Contributing

Expand Down
226 changes: 226 additions & 0 deletions math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
# math

The math module is used to provide helper for high-precision calculations.

## Usage

All the following modules are exposed in `mod.ts`

### Big

A class for high-precision calculations.

All math methods base on `Big` class.

```ts
import { Big } from "https://deno.land/std/math/mod.ts";

new Big(0.1).plus(0.2).toString(); // '0.3'
```

### abs

get a numeric absolute value.

```ts
import { abs } from "https://deno.land/std/math/mod.ts";

abs(-1); // '1'
abs("-0.1"); // '0.1'
```

### min

get a smaller numeric from a numeric set. Similar to `Math.min`.

```ts
import { min } from "https://deno.land/std/math/mod.ts";

min([-1, 0, "1"]); // '-1'
```

### max

get a larger numeric from a numeric set. Similar to `Math.max`.

```ts
import { max } from "https://deno.land/std/math/mod.ts";

max([-1, 0, "1"]); // '1'
```

### sum

get the sum of a numeric set.

```ts
import { sum } from "https://deno.land/std/math/mod.ts";

sum([1, "2", 3]); // '6'
```

### plus

get the value of `a numeric` plus `another numeric`. Similar to Javascript `+` operator.

```ts
import { plus } from "https://deno.land/std/math/mod.ts";

plus("1", 2); // '3'
```

### minus

get the value of `a numeric` minus `another numeric`. Similar to Javascript `-` operator.

```ts
import { minus } from "https://deno.land/std/math/mod.ts";

minus("1", 2); // '-1'
```

### times

get the value of `a numeric` times `another numeric`. Similar to Javascript `*` operator.

```ts
import { times } from "https://deno.land/std/math/mod.ts";

times("1", 2); // '2'
```

### div

get the value of `a numeric` divided `another numeric`. Similar to Javascript `/` operator.

```ts
import { div } from "https://deno.land/std/math/mod.ts";

div("1", 2); // '0.5'
```

### mod

get the value of `a numeric` modulo `another numeric`. Similar to Javascript `%` operator.

```ts
import { mod } from "https://deno.land/std/math/mod.ts";

mod("3", 2); // '1'
```

### pow

get the value of `a numeric` raised to the power `another numeric`. Similar to `Math.pow`.

```ts
import { pow } from "https://deno.land/std/math/mod.ts";

pow("3", 2); // '9'
```

### sqrt

get the value is the square root of `a numeric`. Similar to `Math.sqrt`.

```ts
import { sqrt } from "https://deno.land/std/math/mod.ts";

sqrt("3", 2); // '1.7320508075688772'
```

### round

get the value of input rounded using rounding mode `rm` to a maximum of `dp` decimal places. Similar to `Math.round`.

```ts
import { round } from "https://deno.land/std/math/mod.ts";

round("3.456", 2); // '3.46'
```

### toExponential

get a exponential notation string from `a numeric`. Similar to `Number.prototype.toExponential`.

```ts
import { toExponential } from "https://deno.land/std/math/mod.ts";

toExponential("3.456", 2); // '3.46e+0'
```

### toFixed

get a normal notation string from `a numeric`. Similar to `Number.prototype.toFixed`.

```ts
import { toFixed } from "https://deno.land/std/math/mod.ts";

toFixed("3.4", 6); // '3.400000'
```

### toPrecision

get the value of `a numeric` to the specified number of `sd` significant digits. Similar to `Number.prototype.toPrecision`.

```ts
import { toPrecision } from "https://deno.land/std/math/mod.ts";

toPrecision("3.4567890", 6); // '3.456789'
```

### eq

Where `a numeric` equal to `another numeric`. Similar to Javascript `===` operator.

```ts
import { eq } from "https://deno.land/std/math/mod.ts";

eq("1.200", "1.2e+0"); // true
```

### gt

Where `a numeric` greater then `another numeric`. Similar to Javascript `>` operator.

```ts
import { gt } from "https://deno.land/std/math/mod.ts";

gt(2, "1"); // true
```

### gte

Where `a numeric` greater then or equal to `another numeric`. Similar to Javascript `>=` operator.

```ts
import { gte } from "https://deno.land/std/math/mod.ts";

gte(2, "1"); // true
gte(2, "2"); // true
gte(2, "3"); // false
```

### lt

Where `a numeric` less then `another numeric`. Similar to Javascript `<` operator.

```ts
import { lt } from "https://deno.land/std/math/mod.ts";

lt(2, "1"); // false
lt(2, "2"); // false
lt(2, "3"); // false
```

### lte

Where `a numeric` less then or equal to `another numeric`. Similar to Javascript `<=` operator.

```ts
import { lte } from "https://deno.land/std/math/mod.ts";

lte(2, "1"); // false
lte(2, "2"); // true
lte(2, "3"); // false
```
12 changes: 12 additions & 0 deletions math/abs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

import { BigSource, Big } from "./big/mod.ts";

/**
* Returns the absolute value of a `numberic`
* @param value a type in `BigSource`
* @returns absolute value string
*/
export function abs(value: BigSource): string {
return (value instanceof Big ? value : new Big(value)).abs().toString();
}
Loading