Skip to content

Commit

Permalink
Trig functions (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jscheiny authored May 30, 2018
1 parent 820925b commit 24691b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/measure/__test__/mathTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { meters, seconds } from "../../unit/base";
import { degrees, pi } from "../../unit/angle";
import { meters, radians, seconds } from "../../unit/base";
import * as SafeMath from "../math";
import { Measure } from "../measure";

Expand All @@ -12,6 +13,18 @@ describe("Math", () => {
expect(SafeMath.divide(Measure.of(50, meters), Measure.of(10, seconds))).toEqual(Measure.of(5, mps));
});

it("trig", () => {
const zeroRadians = Measure.of(0, radians);
const zero = Measure.dimensionless(0);
expect(SafeMath.cos(zeroRadians)).toEqual(Measure.dimensionless(1));
expect(SafeMath.sin(zeroRadians)).toEqual(zero);
expect(SafeMath.tan(zeroRadians)).toEqual(zero);
expect(SafeMath.acos(zero)).toEqual(Measure.of(0.5, pi));
expect(SafeMath.asin(zero)).toEqual(zeroRadians);
expect(SafeMath.atan(zero)).toEqual(zeroRadians);
expect(SafeMath.atan2(Measure.of(5, meters), Measure.of(5, meters))).toEqual(Measure.of(45, degrees));
});

it("abs", () => {
expect(SafeMath.abs(Measure.of(-10, mps))).toEqual(Measure.of(10, mps));
});
Expand Down
24 changes: 24 additions & 0 deletions src/measure/math.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { Exponent } from "../exponent/common";
import { Dimensionless, Length, PlaneAngle } from "../quantity/quantities";
import { radians } from "../unit/base";
import { Measure } from "./measure";
import { DivideUnits, ExponentiateUnit, MultiplyUnits, NthRootableUnit, NthRootUnit, Unit } from "./types";
import { nthRootUnit } from "./units";

export const abs = wrapUnary(Math.abs);
export const acos = wrapInverseTrig(Math.acos);
export const asin = wrapInverseTrig(Math.asin);
export const atan = wrapInverseTrig(Math.atan);
export const ceil = wrapUnary(Math.ceil);
export const cos = wrapTrig(Math.cos);
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 sin = wrapTrig(Math.sin);
export const tan = wrapTrig(Math.tan);
export const trunc = wrapUnary(Math.trunc);

export function atan2(y: Length, x: Length): PlaneAngle {
return Measure.of(Math.atan2(y.value, x.value), radians);
}

export function pow<U extends Unit, Y extends Exponent>(x: Measure<U>, y: Y): Measure<ExponentiateUnit<U, Y>> {
return x.toThe(y);
}
Expand Down Expand Up @@ -61,6 +73,18 @@ function wrapUnary(f: (x: number) => number) {
};
}

function wrapTrig(f: (x: number) => number) {
return (angle: PlaneAngle): Dimensionless => {
return Measure.dimensionless(f(angle.value));
};
}

function wrapInverseTrig(f: (x: number) => number) {
return (angle: Dimensionless): PlaneAngle => {
return Measure.of(f(angle.value), radians);
};
}

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());
Expand Down

0 comments on commit 24691b2

Please sign in to comment.