-
-
Notifications
You must be signed in to change notification settings - Fork 30
Numbers
The concept of BigNumber
We use the BigNumber library to do most of our computation. In this docs, if you see a comment with => BigNumber, the output of the function will be a BigNumber instance.
You can create any BigNumber using t.n(x) (where x is your number)
It has the convenience of being able to do float operations without any errors:
0.2 + 0.4 // 0.6000000000000001
t.n(0.2).add(0.4) // 0.6 => BigNumberWe strongly recommend you checking the BigNumber.js website to understand how to use it.
You can round, floor or ceil a number like:
t.round(5.2) // 5 => BigNumber
t.floor(5.7) // 5 => BigNumber
t.ceil(5.2) // 6 => BigNumberYou can get any constants like:
t.c("pi") // 3.141592653589793 => BigNumber
t.c("e", 4) // 2.7182 => BigNumberThese are all the constants:
| Name | Value |
|---|---|
| pi | 3.14159265 |
| e | 2.71828182 |
| sqrt(2) | 1.41421356 |
| goldenRatio | 1.61803398 |
| EulerGamma | 0.57721566 |
| UltimateAnswer | 42 |
All constants are stored with 250 digits
You can compute constants such as pi, e or the goldenRatio. Computing these value may take a lot more time.
For example, computing 1000 digits of pi took me around 19 seconds on my MacBook Pro.
Here is how you use it:
t.pi() // 3.1415926535897932408 => BigNumber
t.pi(4) // 3.14159264 => BigNumber
t.e() // 2.71828182845904523536 => BigNumber
t.goldenRatio() // 1.618033988749895 => BigNumberNote that you can't compute more than 15 digits of the golden ratio, because of a BigNumber limitation.
You can check pretty efficiently if a number is prime or not like that:
t.isPrime(2011) // trueIf your number is superior to
Number.MAX_SAFE_INTEGER, it won't work
You can get the least factor (that is not 1) of any number n inferior to Number.MAX_SAFE_INTEGER like:
t.leastFactor(50) // 2 => BigNumberAny questions? Don't hesitate to create an issue and tell me about your problem 😊.
Copyright © 2017-2018 Arthur Guiot