Native BigInt in Javascript #2737
Replies: 7 comments 1 reply
-
There could also be the possibility of implementing a very simple BigDecimal library which uses BigInts under the hood for exceptional performance. I will look into this! |
Beta Was this translation helpful? Give feedback.
-
Yes, really cool! Maybe we should simply introduce this as a separate, new data type Having a |
Beta Was this translation helpful? Give feedback.
-
I've published a small library that implements big decimal numbers using the new https://github.com/ericman314/bignum-native If anyone thinks it might have promise I would sure appreciate any contributions you can make. |
Beta Was this translation helpful? Give feedback.
-
Thanks for sharing Eric! |
Beta Was this translation helpful? Give feedback.
-
It would be great if somehow users could pick their big number implementation. |
Beta Was this translation helpful? Give feedback.
-
Here is a small proof of concept of using BigInt in mathjs: http://jsbin.com/kikokot/edit?html,output <!DOCTYPE html>
<html>
<head>
<meta name="description" content="math.js | bigint demo">
<title>math.js | bigint demo</title>
<script src="https://unpkg.com/[email protected]/dist/math.min.js"></script>
</head>
<body>
<h1>mathjs with BigInt demo</h1>
<p>
Only works on the latest version of Chrome right now
</p>
<p id="addResult"></p>
<p id="powResult"></p>
<p id="error" style="color: red"></p>
<script>
try {
math.import({
name: 'BigInt',
path: 'type', // will be imported into math.type.BigInt
factory: (type, config, load, typed) => {
typed.addType({
name: 'BigInt',
test: (x) => typeof x === 'bigint'
})
// we can also add conversions here from number or string to BigInt
// and vice versa using typed.addConversion(...)
return BigInt
},
lazy: false // disable lazy loading as this factory has side
// effects: it adds a type and a conversion.
})
math.import({
name: 'bigint',
factory: (type, config, load, typed) => {
return typed('bigint', {
'number | string ': (x) => BigInt(x)
})
}
})
math.import({
name: 'add',
factory: (type, config, load, typed) => {
return typed('add', {
'BigInt, BigInt': (a, b) => a + b
})
}
})
math.import({
name: 'pow',
factory: (type, config, load, typed) => {
return typed('pow', {
'BigInt, BigInt': (a, b) => a ** b
})
}
})
document.getElementById('addResult').innerHTML =
'4349 + 5249 = ' + math.eval('bigint(4349) + bigint(5249)')
document.getElementById('powResult').innerHTML =
'4349 ^ 5249 = ' + math.eval('bigint(4349) ^ bigint(5249)')
}
catch (err) {
console.error(err)
document.getElementById('error').innerHTML = err.toString()
}
</script>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
-
Are there any news/plans for BigInt support? |
Beta Was this translation helpful? Give feedback.
-
Chrome 67 will ship with native support for the
BigInt
type, arbitrary precision itegers. I opened this issue to discuss how we might useBigInt
s once adoption is more widespread.There's no
BigDecimal
implementation yet, butBigInt
could be particularly useful for fractions.Related: #15, #604
Beta Was this translation helpful? Give feedback.
All reactions