Skip to content

Numbers

Arthur Guiot edited this page May 18, 2018 · 9 revisions

Round, floor, and ceil

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 => BigNumber

Constants

Get constants

You can get any constants like:

t.c("pi") // 3.141592653589793 => BigNumber
t.c("e", 4) // 2.7182 => BigNumber

These are all the constants:

Name Value
alphaParticleMass 6.64465675e-27
atomicMass 1.66053892e-27
Avogadro 6.02214129e23
Boltzmann 1.3806488e-23
conductanceQuantum 7.74809173e-5
e 2.71828182
earth-moon 384401
earth-sun 1.496e8
earthMass 5.974e+24
earthRadius 6378
electric 8.854187e-12
electronMass 9.10938291e-31
elementaryCharge 1.60217656e-19
EulerGamma 0.57721566
Faraday 96485.3365
fineStructure 7.29735256e-3
goldenRatio 1.61803398
gravity 9.80665
inverseFineStructure 137.035999
magnetic 12.5663706e-7
magneticFluxQuantum 2.06783375e-15
molarGas 8.3144621
moonMass 7.348e22
moonRadius 1738
neutronMass 1.67492735e-27
NewtonGravitation 6.67384e-11
pi 3.14159265
Planck 6.62606957e-34
proton-electronMassRatio 1836.15267
proton-neutronMassRatio 0.99862347
protonMass 1.67262177e-27
Rydberg 10973731.5
speedOfLight 299792458
speedOfSound 340.27
sqrt(2) 1.41421356
Stefan-Boltzmann 5.670373e-8
sunMass 1.989e30
sunRadius 695500
TheRockMass 124.737901
ThomsonCrossSection 0.66524587e-28
UltimateAnswer 42
zeroKelvin -273.15

Constants have a precision limit of 250 digits

Compute constants

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 => BigNumber

Note that you can't compute more than 15 digits of the golden ratio, because of a BigNumber limitation.

Is Prime

You can check pretty efficiently if a number is prime or not like that:

t.isPrime(2011) // true

If your number is superior to Number.MAX_SAFE_INTEGER, it won't work

Least Factor

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 => BigNumber
Clone this wiki locally