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