Skip to content

Commit a4ee4e1

Browse files
committed
add asin, acos
1 parent cdf52c2 commit a4ee4e1

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ function complement(A: Union | Interval): Union;
248248
| Cosine | `nsf.cos(A)` |
249249
| Sine | `nsf.sin(A)` |
250250
| Tangent | `nsf.tan(A)` |
251+
| Arccos | `nsf.acos(A)` |
252+
| Arcsin | `nsf.asin(A)` |
251253

252254
</div>
253255

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export { overlap, disjoint, intersection, complement } from "./compare.ts";
44
export { Union, union, single, bounded, EMPTY, FULL } from "./union.ts";
55
export { add, sub, mul, div, neg } from "./arithmetic.ts";
66
export { abs, min, max, exp, log, pow, powInt, sqrt, sqinv, powIntInv } from "./math.ts";
7-
export { cos, sin, tan } from "./trigonometry.ts";
7+
export { cos, sin, tan, acos, asin } from "./trigonometry.ts";

src/trigonometry.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { prev, next } from "./nextafter.ts";
22
import { Interval, typeCheckIsInterval } from "./interval.ts";
3-
import { Union, typeCheckIsUnion, makeUnaryOpUnion, makeUnaryOpEither, bounded } from "./union.ts";
3+
import {
4+
Union,
5+
typeCheckIsUnion,
6+
makeUnaryOpUnion,
7+
makeUnaryOpEither,
8+
bounded,
9+
EMPTY,
10+
} from "./union.ts";
411
import { udiv, usub } from "./arithmetic.ts";
512

613
const pi = Math.PI;
@@ -105,3 +112,30 @@ export function utan(U: Union): Union {
105112
}
106113

107114
export const tan = makeUnaryOpEither(utan);
115+
116+
// ARCCOS
117+
118+
export function iacos(X: Interval): Union {
119+
typeCheckIsInterval(X);
120+
121+
if (X.lo > 1 || X.hi < -1) return EMPTY;
122+
const lower = X.hi >= 1 ? 0 : prev(Math.acos(X.hi));
123+
const upper = next(Math.acos(Math.max(-1, X.lo)));
124+
return new Union([new Interval(lower, upper)]);
125+
}
126+
127+
export const uacos = makeUnaryOpUnion(iacos);
128+
export const acos = makeUnaryOpEither(uacos);
129+
130+
// ARCSIN
131+
132+
export function iasin(X: Interval): Union {
133+
typeCheckIsInterval(X);
134+
135+
if (X.lo > 1 || X.hi < -1) return EMPTY;
136+
const [l, h] = [Math.max(-1, X.lo), Math.min(1, X.hi)];
137+
return new Union([new Interval(prev(Math.asin(l)), next(Math.asin(h)))]);
138+
}
139+
140+
export const uasin = makeUnaryOpUnion(iasin);
141+
export const asin = makeUnaryOpEither(uasin);

tests/trigonometry.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,87 @@ describe("tan", () => {
221221
checkTan(nsf.bounded(Math.PI / 6));
222222
});
223223
});
224+
225+
describe("acos", () => {
226+
assert.deepStrictEqual(
227+
nsf.acos(nsf.single(0)),
228+
nsf.single(prev(Math.acos(0)), next(Math.acos(0)))
229+
);
230+
231+
assert.deepStrictEqual(
232+
nsf.acos(nsf.single(-0.1)),
233+
nsf.single(prev(Math.acos(-0.1)), next(Math.acos(-0.1)))
234+
);
235+
236+
assert.deepStrictEqual(
237+
nsf.acos(nsf.single(0.2)),
238+
nsf.single(prev(Math.acos(0.2)), next(Math.acos(0.2)))
239+
);
240+
241+
assert.deepStrictEqual(
242+
nsf.acos(nsf.single(0.2, 0.5)),
243+
nsf.single(prev(Math.acos(0.5)), next(Math.acos(0.2)))
244+
);
245+
246+
assert.deepStrictEqual(
247+
nsf.acos(nsf.single(-0.2, 0.5)),
248+
nsf.single(prev(Math.acos(0.5)), next(Math.acos(-0.2)))
249+
);
250+
251+
assert.deepStrictEqual(nsf.acos(nsf.single(-1.1, 1.1)), nsf.single(0, next(Math.acos(-1))));
252+
253+
assert.deepStrictEqual(
254+
nsf.acos(nsf.single(-1.1, -0.5)),
255+
nsf.single(prev(Math.acos(-0.5)), next(Math.acos(-1)))
256+
);
257+
258+
assert.deepStrictEqual(nsf.acos(nsf.single(0.5, 1.5)), nsf.single(0, next(Math.acos(0.5))));
259+
260+
assert.deepStrictEqual(nsf.acos(nsf.single(-1.2, -1.1)), nsf.EMPTY);
261+
assert.deepStrictEqual(nsf.acos(nsf.single(1.1, 1.2)), nsf.EMPTY);
262+
});
263+
264+
describe("asin", () => {
265+
assert.deepStrictEqual(
266+
nsf.asin(nsf.single(0)),
267+
nsf.single(prev(Math.asin(0)), next(Math.asin(0)))
268+
);
269+
270+
assert.deepStrictEqual(
271+
nsf.asin(nsf.single(-0.1)),
272+
nsf.single(prev(Math.asin(-0.1)), next(Math.asin(-0.1)))
273+
);
274+
275+
assert.deepStrictEqual(
276+
nsf.asin(nsf.single(0.2)),
277+
nsf.single(prev(Math.asin(0.2)), next(Math.asin(0.2)))
278+
);
279+
280+
assert.deepStrictEqual(
281+
nsf.asin(nsf.single(0.2, 0.5)),
282+
nsf.single(prev(Math.asin(0.2)), next(Math.asin(0.5)))
283+
);
284+
285+
assert.deepStrictEqual(
286+
nsf.asin(nsf.single(-0.2, 0.5)),
287+
nsf.single(prev(Math.asin(-0.2)), next(Math.asin(0.5)))
288+
);
289+
290+
assert.deepStrictEqual(
291+
nsf.asin(nsf.single(-1.1, 1.1)),
292+
nsf.single(prev(Math.asin(-1)), next(Math.asin(1)))
293+
);
294+
295+
assert.deepStrictEqual(
296+
nsf.asin(nsf.single(-1.1, -0.5)),
297+
nsf.single(prev(Math.asin(-1)), next(Math.asin(-0.5)))
298+
);
299+
300+
assert.deepStrictEqual(
301+
nsf.asin(nsf.single(0.5, 1.5)),
302+
nsf.single(prev(Math.asin(0.5)), next(Math.asin(1)))
303+
);
304+
305+
assert.deepStrictEqual(nsf.asin(nsf.single(-1.2, -1.1)), nsf.EMPTY);
306+
assert.deepStrictEqual(nsf.asin(nsf.single(1.1, 1.2)), nsf.EMPTY);
307+
});

0 commit comments

Comments
 (0)