-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pull out math function into separate file * Separate math tests file * Export safe math * Update readme * Remove trig functions
- Loading branch information
Showing
6 changed files
with
150 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { meters, seconds } from "../../unit"; | ||
import * as SafeMath from "../math"; | ||
import { Measure } from "../measure"; | ||
|
||
describe("Math", () => { | ||
const mps = meters.per(seconds); | ||
|
||
it("arithmetic", () => { | ||
expect(SafeMath.add(Measure.of(5, mps), Measure.of(-5, mps))).toEqual(Measure.of(0, mps)); | ||
expect(SafeMath.subtract(Measure.of(5, mps), Measure.of(-5, mps))).toEqual(Measure.of(10, mps)); | ||
expect(SafeMath.multiply(Measure.of(5, mps), Measure.of(10, seconds))).toEqual(Measure.of(50, meters)); | ||
expect(SafeMath.divide(Measure.of(50, meters), Measure.of(10, seconds))).toEqual(Measure.of(5, mps)); | ||
}); | ||
|
||
it("abs", () => { | ||
expect(SafeMath.abs(Measure.of(-10, mps))).toEqual(Measure.of(10, mps)); | ||
}); | ||
|
||
it("cbrt", () => { | ||
expect(SafeMath.cbrt(Measure.of(64, seconds.cubed()))).toEqual(Measure.of(4, seconds)); | ||
}); | ||
|
||
it("ceil", () => { | ||
expect(SafeMath.ceil(Measure.of(3.4, mps))).toEqual(Measure.of(4, mps)); | ||
}); | ||
|
||
it("floor", () => { | ||
expect(SafeMath.floor(Measure.of(7.8, mps))).toEqual(Measure.of(7, mps)); | ||
}); | ||
|
||
it("hypot", () => { | ||
expect(SafeMath.hypot(Measure.of(3, meters), Measure.of(4, meters))).toEqual(Measure.of(5, meters)); | ||
}); | ||
|
||
it("max", () => { | ||
expect(SafeMath.max(Measure.of(10, mps), Measure.of(5, mps), Measure.of(15, mps))).toEqual(Measure.of(15, mps)); | ||
}); | ||
|
||
it("min", () => { | ||
expect(SafeMath.min(Measure.of(10, mps), Measure.of(5, mps), Measure.of(15, mps))).toEqual(Measure.of(5, mps)); | ||
}); | ||
|
||
it("pow", () => { | ||
expect(SafeMath.pow(Measure.of(10, meters), 3)).toEqual(Measure.of(1000, meters.cubed())); | ||
}); | ||
|
||
it("round", () => { | ||
expect(SafeMath.round(Measure.of(7.8, mps))).toEqual(Measure.of(8, mps)); | ||
}); | ||
|
||
it("sqrt", () => { | ||
expect(SafeMath.sqrt(Measure.of(25, meters.squared()))).toEqual(Measure.of(5, meters)); | ||
}); | ||
|
||
it("sum", () => { | ||
expect(SafeMath.sum(Measure.of(10, mps), Measure.of(5, mps), Measure.of(15, mps))).toEqual(Measure.of(30, mps)); | ||
}); | ||
|
||
it("trunc", () => { | ||
expect(SafeMath.trunc(Measure.of(-7.8, mps))).toEqual(Measure.of(-7, mps)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import * as SafeMath from "./math"; | ||
import { Unit } from "./units"; | ||
|
||
export { Unit }; | ||
export { SafeMath, Unit }; | ||
export * from "./measure"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Exponent } from "../exponent"; | ||
import { Measure } from "./measure"; | ||
import { DivideUnits, ExponentiateUnit, MultiplyUnits, NthRootableUnit, NthRootUnit } from "./types"; | ||
import { nthRootUnit, Unit } from "./units"; | ||
|
||
export const abs = wrapUnary(Math.abs); | ||
export const ceil = wrapUnary(Math.ceil); | ||
export const floor = wrapUnary(Math.floor); | ||
export const fround = wrapUnary(Math.fround); | ||
export const hypot = warpNary(Math.hypot); | ||
export const max = warpNary(Math.max); | ||
export const min = warpNary(Math.min); | ||
export const round = wrapUnary(Math.round); | ||
export const trunc = wrapUnary(Math.trunc); | ||
|
||
export function pow<U extends Unit, Y extends Exponent>(x: Measure<U>, y: Y): Measure<ExponentiateUnit<U, Y>> { | ||
return x.toThe(y); | ||
} | ||
|
||
export function sqrt<U extends NthRootableUnit<2>>(x: Measure<U>): Measure<NthRootUnit<U, 2>> { | ||
return Measure.unsafeConstruct(Math.sqrt(x.value), nthRootUnit(x.getUnit(), 2)); | ||
} | ||
|
||
export function cbrt<U extends NthRootableUnit<3>>(x: Measure<U>): Measure<NthRootUnit<U, 3>> { | ||
return Measure.unsafeConstruct(Math.cbrt(x.value), nthRootUnit(x.getUnit(), 3)); | ||
} | ||
|
||
export function add<U extends Unit>(left: Measure<U>, right: Measure<U>): Measure<U> { | ||
return left.plus(right); | ||
} | ||
|
||
export function subtract<U extends Unit>(left: Measure<U>, right: Measure<U>): Measure<U> { | ||
return left.minus(right); | ||
} | ||
|
||
export function multiply<L extends Unit, R extends Unit>( | ||
left: Measure<L>, | ||
right: Measure<R>, | ||
): Measure<MultiplyUnits<L, R>> { | ||
return left.times(right); | ||
} | ||
|
||
export function divide<L extends Unit, R extends Unit>( | ||
left: Measure<L>, | ||
right: Measure<R>, | ||
): Measure<DivideUnits<L, R>> { | ||
return left.over(right); | ||
} | ||
|
||
export function sum<U extends Unit>(first: Measure<U>, ...rest: Array<Measure<U>>): Measure<U> { | ||
let result = first; | ||
for (const measure of rest) { | ||
result = result.plus(measure); | ||
} | ||
return result; | ||
} | ||
|
||
function wrapUnary(f: (x: number) => number) { | ||
return <U extends Unit>(x: Measure<U>): Measure<U> => { | ||
return Measure.of(f(x.value), x.normalized()); | ||
}; | ||
} | ||
|
||
function warpNary(f: (...x: number[]) => number) { | ||
return <U extends Unit>(first: Measure<U>, ...rest: Array<Measure<U>>): Measure<U> => { | ||
return Measure.of(f(...values(first, ...rest)), first.normalized()); | ||
}; | ||
} | ||
|
||
function values<U extends Unit>(...measures: Array<Measure<U>>): number[] { | ||
return measures.map(measure => measure.value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters