FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/examples/bignumbers.js at develop · Dionuta/mathjs · GitHub
Dionuta
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
examples
/
bignumbers.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (39 loc) · 1.63 KB
Breadcrumbs
mathjs
/
examples
/
bignumbers.js
Copy path
File metadata and controls
47 lines (39 loc) · 1.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// BigNumbers
// load math.js (using node.js)
const
math
=
require
(
'../index'
)
// configure the default type of numbers as BigNumbers
math
.
config
(
{
number
:
'BigNumber'
,
// Default type of number:
// 'number' (default), 'BigNumber', or 'Fraction'
precision
:
20
// Number of significant digits for BigNumbers
}
)
console
.
log
(
'round-off errors with numbers'
)
print
(
math
.
add
(
0.1
,
0.2
)
)
// number, 0.30000000000000004
print
(
math
.
divide
(
0.3
,
0.2
)
)
// number, 1.4999999999999998
console
.
log
(
)
console
.
log
(
'no round-off errors with BigNumbers'
)
print
(
math
.
add
(
math
.
bignumber
(
0.1
)
,
math
.
bignumber
(
0.2
)
)
)
// BigNumber, 0.3
print
(
math
.
divide
(
math
.
bignumber
(
0.3
)
,
math
.
bignumber
(
0.2
)
)
)
// BigNumber, 1.5
console
.
log
(
)
console
.
log
(
'create BigNumbers from strings when exceeding the range of a number'
)
print
(
math
.
bignumber
(
1.2e+500
)
)
// BigNumber, Infinity WRONG
print
(
math
.
bignumber
(
'1.2e+500'
)
)
// BigNumber, 1.2e+500
console
.
log
(
)
console
.
log
(
'BigNumbers still have a limited precision and are no silve bullet'
)
const
third
=
math
.
divide
(
math
.
bignumber
(
1
)
,
math
.
bignumber
(
3
)
)
const
total
=
math
.
add
(
third
,
third
,
third
)
print
(
total
)
// BigNumber, 0.99999999999999999999
console
.
log
(
)
// one can work conveniently with BigNumbers using the expression parser.
// note though that BigNumbers are only supported in arithmetic functions
console
.
log
(
'use BigNumbers in the expression parser'
)
print
(
math
.
eval
(
'0.1 + 0.2'
)
)
// BigNumber, 0.3
print
(
math
.
eval
(
'0.3 / 0.2'
)
)
// BigNumber, 1.5
console
.
log
(
)
/**
* Helper function to output a value in the console. Value will be formatted.
*
@param
{
*
} value
*/
function
print
(
value
)
{
console
.
log
(
math
.
format
(
value
)
)
}
Back
|
FazBrowse Home
|
New Git URL