UNPKG

1.95 kBMarkdownView Raw
1# Data Types
2
3The functions of math.js support multiple data types, both native JavaScript
4types as well as more advanced types implemented in math.js. The data types can
5be mixed together in calculations, for example by adding a Number to a
6Complex number or Array.
7
8The supported data types are:
9
10- Boolean
11- [Number](numbers.md)
12- [BigNumber](bignumbers.md)
13- [Complex](complex_numbers.md)
14- [Fraction](fractions.md)
15- [Array](matrices.md)
16- [Matrix](matrices.md)
17- [Unit](units.md)
18- String
19
20Function [`math.typeOf(x)`](../reference/functions/typeOf.md) can be used to get
21the type of a variable.
22
23Example usage:
24
25```js
26// use numbers
27math.subtract(7.1, 2.3) // 4.8
28math.round(math.pi, 3) // 3.142
29math.sqrt(4.41e2) // 21
30
31// use BigNumbers
32math.add(math.bignumber(0.1), math.bignumber(0.2)) // BigNumber, 0.3
33
34// use Fractions
35math.add(math.fraction(1), math.fraction(3)) // Fraction, 0.(3)
36
37// use strings
38math.add('hello ', 'world') // 'hello world'
39math.max('A', 'D', 'C') // 'D'
40
41// use complex numbers
42const a = math.complex(2, 3) // 2 + 3i
43a.re // 2
44a.im // 3
45const b = math.complex('4 - 2i') // 4 - 2i
46math.add(a, b) // 6 + i
47math.sqrt(-4) // 2i
48
49// use arrays
50const array = [1, 2, 3, 4, 5]
51math.factorial(array) // Array, [1, 2, 6, 24, 120]
52math.add(array, 3) // Array, [3, 5, 6, 7, 8]
53
54// use matrices
55const matrix = math.matrix([1, 4, 9, 16, 25]) // Matrix, [1, 4, 9, 16, 25]
56math.sqrt(matrix) // Matrix, [1, 2, 3, 4, 5]
57
58// use units
59const a = math.unit(55, 'cm') // 550 mm
60const b = math.unit('0.1m') // 100 mm
61math.add(a, b) // 0.65 m
62
63// check the type of a variable
64math.typeOf(2) // 'number'
65math.typeOf(math.unit('2 inch')) // 'Unit'
66math.typeOf(math.sqrt(-4)) // 'Complex'
67```