UNPKG

1.34 kBJavaScriptView Raw
1// This example demonstrates how you could integrate support for BigInt
2// in mathjs. It's just a proof of concept, for full support you will
3// have to defined more functions and define conversions from and to
4// other data types.
5
6const { create, all, factory } = require('../..')
7const math = create(all)
8
9// we can also add conversions here from number or string to BigInt
10// and vice versa using math.typed.addConversion(...)
11
12math.import([
13 factory('BigInt', ['typed'], function createBigInt ({ typed }) {
14 typed.addType({
15 name: 'BigInt',
16 test: (x) => typeof x === 'bigint' // eslint-disable-line
17 })
18
19 return BigInt // eslint-disable-line
20 }, { lazy: false }),
21
22 factory('bigint', ['typed', 'BigInt'], function createBigint ({ typed, BigInt }) {
23 return typed('bigint', {
24 'number | string ': (x) => BigInt(x) // eslint-disable-line
25 })
26 }),
27
28 factory('add', ['typed'], function createBigIntAdd ({ typed }) {
29 return typed('add', {
30 'BigInt, BigInt': (a, b) => a + b
31 })
32 }),
33
34 factory('pow', ['typed'], function createBigIntPow ({ typed }) {
35 return typed('pow', {
36 'BigInt, BigInt': (a, b) => a ** b
37 })
38 })
39])
40
41console.log(math.evaluate('4349 + 5249'))
42console.log(math.evaluate('bigint(4349) + bigint(5249)'))
43console.log(math.evaluate('bigint(4349) ^ bigint(5249)'))