UNPKG

1.84 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2import { typeOf } from '../../utils/is'
3
4const name = 'divideScalar'
5const dependencies = ['typed', 'numeric']
6
7export const createDivideScalar = /* #__PURE__ */ factory(name, dependencies, ({ typed, numeric }) => {
8 /**
9 * Divide two scalar values, `x / y`.
10 * This function is meant for internal use: it is used by the public functions
11 * `divide` and `inv`.
12 *
13 * This function does not support collections (Array or Matrix).
14 *
15 * @param {number | BigNumber | Fraction | Complex | Unit} x Numerator
16 * @param {number | BigNumber | Fraction | Complex} y Denominator
17 * @return {number | BigNumber | Fraction | Complex | Unit} Quotient, `x / y`
18 * @private
19 */
20 const divideScalar = typed(name, {
21 'number, number': function (x, y) {
22 return x / y
23 },
24
25 'Complex, Complex': function (x, y) {
26 return x.div(y)
27 },
28
29 'BigNumber, BigNumber': function (x, y) {
30 return x.div(y)
31 },
32
33 'Fraction, Fraction': function (x, y) {
34 return x.div(y)
35 },
36
37 'Unit, number | Fraction | BigNumber': function (x, y) {
38 const res = x.clone()
39 // TODO: move the divide function to Unit.js, it uses internals of Unit
40 const one = numeric(1, typeOf(y))
41 res.value = divideScalar(((res.value === null) ? res._normalize(one) : res.value), y)
42 return res
43 },
44
45 'number | Fraction | BigNumber, Unit': function (x, y) {
46 let res = y.clone()
47 res = res.pow(-1)
48 // TODO: move the divide function to Unit.js, it uses internals of Unit
49 const one = numeric(1, typeOf(x))
50 res.value = divideScalar(x, ((y.value === null) ? y._normalize(one) : y.value))
51 return res
52 },
53
54 'Unit, Unit': function (x, y) {
55 return x.divide(y)
56 }
57 })
58
59 return divideScalar
60})