Published
Edited
Jul 19, 2019
4 stars
Insert cell
Insert cell
Insert cell
RationalAngleAccumulator = () => {
const
running_angle = Ratio(0, 1),
running_angle_int = running_angle[int32buffer],
angle = Ratio(0, 1),
angle_int = angle[int32buffer],

compose_angle = (numerator, denominator=1, half_turns=0) => {
angle[0] = numerator;
angle[1] = denominator;
angle_int[4] = half_turns;
normalize_ratio(angle);
const rp = running_angle[0], rq = running_angle[1]; // temporary copy
running_angle[0] = angle[0] * rq + rp * angle[1];
running_angle[1] = rq * angle[1] - angle[0] * rp;
running_angle_int[4] += angle_int[4]; // sum winding numbers
normalize_ratio(running_angle);
},

unit_complex = () => {
const
out = new Float64Array(2),
half_turn = 1 - ((running_angle_int[4] & 1) << 1),
scale = half_turn / Math.hypot(running_angle[0], running_angle[1]);
out[0] = running_angle[1] * scale, out[1] = running_angle[0] * scale;
return out;
},

tangent = () => {
return running_angle[0] / running_angle[1];
},

angle_measure = () => {
return (
Math.PI * running_angle_int[4] +
Math.atan2(running_angle[0], running_angle[1]));
},

angle_measure_degrees = () => {
return (
180 * running_angle_int[4] +
DEGREES * Math.atan2(running_angle[0], running_angle[1]));
};

return {
running_angle,
compose_angle,
unit_complex,
tangent,
angle_measure,
angle_measure_degrees,
}
}
Insert cell
{ // Example
const running_angle = RationalAngleAccumulator();
for (let i = 0; i < 15; i++) {
running_angle.compose_angle(1,1);
}
return [running_angle.angle_measure_degrees(), running_angle.tangent(), running_angle.unit_complex()];
}
Insert cell
normalize_ratio(Ratio(0x10000000000000,1))
Insert cell
normalize_ratio = {
const
EMASK = 0x7ff00000, // 0111 1111 1111 0000 0000 0000 0000 0000
ETOP = 0x7f000000, // 0111 1111 0000 0000 0000 0000 0000 0000
SMASK = 0x80000000, // 1000 0000 0000 0000 0000 0000 0000 0000
ONE = 0x3ff00000, // 0011 1111 1111 0000 0000 0000 0000 0000
offset_float = new Float64Array([1.0]),
offset_int = new Int32Array(offset_float.buffer);
return function normalize_ratio(ratio) {
const
intbuf = ratio[int32buffer],
p = ratio[0],
q = ratio[1],
pe = intbuf[1] & EMASK, // p exponent
qe = intbuf[3] & EMASK, // q exponent
ps = intbuf[1] & SMASK, // p sign
qs = intbuf[3] & SMASK; // q sign
if ((pe === EMASK) | (q === 0)) { // Check for x:0, ∞:x, NaN:x
if (isNaN(p) | (qe === EMASK) | (p === 0)) {
ratio[0] = NaN, ratio[1] = NaN;
return ratio; }
intbuf[0] = 0, intbuf[1] = ONE; // set ratio to 1:0
intbuf[2] = 0, intbuf[3] = 0;
intbuf[4] -= ps >>> 31; // subtract 1 from winding number in -1:0 case
return ratio;
}
intbuf[4] += (qs >>> 31) * (1 - (ps >>> 30)) // add sign of p to winding number if q < 0
if ((qe === EMASK) | (p === 0)) { // Check for 0:x, x:∞, x:NaN
if (isNaN(q) | (pe === EMASK) | (q === 0)) {
ratio[0] = NaN, ratio[1] = NaN;
return ratio; }
intbuf[0] = 0, intbuf[1] = 0;
intbuf[2] = 0, intbuf[3] = ONE; // set ratio to 0:1
return ratio;
}
offset_int[1] = ((ETOP - (pe + qe >>> 1)) & EMASK) + qs;
ratio[0] *= offset_float[0];
ratio[1] *= offset_float[0];
return ratio;
}
}
Insert cell
int32buffer = Symbol('int32buffer');
Insert cell
Ratio = (p, q, half_turns=0) => {
const
intbuf = new Int32Array(5),
ratio = new Float64Array(intbuf.buffer, 0, 2);
ratio[0] = p;
ratio[1] = q;
intbuf[4] = half_turns;
ratio[int32buffer] = intbuf;
return ratio;
}
Insert cell
DEGREES = 180 / Math.PI
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more