๐Number+ext
๐ง under construction
JS โฉ value โฉ primitive โฉ number โฉ +ext
extend Number with custom methods.
๐ custom
replit โฉ Number extension (2)
// 2023.03.04 - 06:50 (+) Quaternion, n.asQuaternion
// 2023.03.01 - 13:30 (/) isEqualTo -> equal, Number.EPSILON -> 1e-10
// (-) areEqualNumbers()
// (+) .round()
// 2023.01.20 - 16:45 (+) deg
// 2023.01.20 - 11:47 (+) .clamp(), .isEqualTo (*first recorded)
// -----------------------------------------------------
const { Random } = require('./Random.js');
const { sqrt, abs } = Math;
const {log} = console;
// deg
const deg = Math.PI / 180;
// โญ Number+ext
// -----------------------------------------------------
// (constants)
// - deg // degree = PI/180
// -----------------------------------------------------
// (quaternion)
// ๐ธ n.asQuaternion
//
// -----------------------------------------------------
// (integer)
// ๐ธ n.isEven
// ๐ธ n.isOdd
// ๐ธ n.binary
// ๐ธ n.octal
// ๐ธ n.hex
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
// ๐น n.toOctal({prefix:, pad: {length:, char:})
// ๐น n.toHex({prefix:, pad: {length:, char:})
// -----------------------------------------------------
// ๐น .clamp()
// ๐น .equal()
// ๐น .round()
//
Object.defineProperties(Number.prototype, {
// ---------------
// quaternion
// ---------------
// ๐ธ n.asQuaternion
asQuaternion: {
get() { return new Quaternion(this) },
},
// ---------------
// integer
// ---------------
// ๐ธ n.isEven
isEven: {
get() { return this % 2 === 0 },
},
// ๐ธ n.isOdd
isOdd: {
get() { return Math.abs(this % 2) === 1 },
},
// ๐ธ n.binary
binary: {
get() { return this.toString(2) },
},
// ๐ธ n.octal
octal: {
get() { return this.toString(8) },
},
// ๐ธ n.hex
hex: {
get() { return this.toString(16).toUpperCase() },
},
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
toBinary: {
value: formatFunc('0b', 2),
},
// ๐น n.toOctal({prefix:, pad: {length:, char:}})
toOctal: {
value: formatFunc('0o', 8),
},
// ๐น n.toHex({prefix:, pad: {length:, char:}})
toHex: {
value: formatFunc('0x', 16),
},
// ---------------
// range
// ---------------
// ๐น .clamp(min, max)
clamp: {
value: function(min, max){
if (min > max) [min, max] = [max, min];
return Math.min(max, Math.max(min, this));
},
},
// ๐น .equal()
equal: {
value: function(y, {threshold=1e-10}={}){
return abs(this - y) < threshold;
},
},
// ๐น .round()
round: {
value: function(places){
return +this.toFixed(places);
},
},
});
// โญ helpers
// -----------------------------------------------------
// general format function
function formatFunc(pre, base) {
return function format({
prefix = pre,
pad = { length: 0, char: '0' },
} = {}) {
// โญ `pad` might be overridden, better destructure again
const { length = 0, char = '0' } = pad;
return prefix +
this.toString(base).padStart(length, char).toUpperCase();
}
}
// 2023.03.04 - 07:19 (+) .scalarPart, .vectorPart
// 2023.03.04 - 06:50 (โข) -------- merged into "Number+ext" --------
// 2023.03.03 - 17:08 (+) .dot, .cross
// 2023.03.01 - 13:42 (+) .randomInt, .randomFloat, .toString
// 2023.02.24 - 15:33 (+) static members, .conjugate, .norm, .inverse, ...
// 2023.02.24 - 13:35 (โข) first draft (by chatGPT)
// -------------------------------------------------
// โญ Quaternion
// --------------------------------------------------
// - Quaternion.zero, .one, .i, .j, .k
// - Quaternion.randomInt()
// - Quaternion.randomFloat()
// --------------------------------------------------
// - .scalarPart, vectorPart
// - .conjugate
// - .normSquare
// - .norm
// - .inverse
// --------------------------------------------------
// - .add() p + q
// - .subtract() p - q
// - .multiply() pq
// - .divide() p/q = p q^(-1)
// - .scale() kp
// - .dot() p โข q
// - .cross() p x q
// --------------------------------------------------
// - .distance() |p-q|
// - .equal() p == q
//
class Quaternion {
// i, j, k
static get zero() { return new Quaternion(0, 0, 0, 0) }
static get one() { return new Quaternion(1, 0, 0, 0) }
static get i() { return new Quaternion(0, 1, 0, 0) }
static get j() { return new Quaternion(0, 0, 1, 0) }
static get k() { return new Quaternion(0, 0, 0, 1) }
// init
constructor(a=0, b=0, c=0, d=0) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
// ----------------------
// static methods
// ----------------------
// Quaternion.randomInt()
static randomInt(min, max) {
return new Quaternion(
Random.int(min, max),
Random.int(min, max),
Random.int(min, max),
Random.int(min, max),
)
}
// Quaternion.randomFloat()
static randomFloat(min, max) {
return new Quaternion(
Random.float(min, max),
Random.float(min, max),
Random.float(min, max),
Random.float(min, max),
)
}
// ----------------------
// getters
// ----------------------
// scalarPart
get scalarPart() {
return this.a;
}
// vectorPart
get vectorPart() {
const { b, c, d } = this;
return new Quaternion(0, b, c, d);
}
// conjugate
get conjugate() {
const { a, b, c, d } = this;
return new Quaternion(a, -b, -c, -d);
}
// inverse (reciprocal)
get inverse() {
const n = this.normSquare;
return this.conjugate.scale(1 / n);
}
// |p|^2 = p โข p
get normSquare() {
return this.dot(this);
}
// norm
get norm() {
const { a, b, c, d } = this;
return sqrt(this.normSquare);
}
// ---------------------------
// โญ + - * / operations
// ---------------------------
add(q) {
const { a, b, c, d } = this;
return new Quaternion(
a + q.a, b + q.b, c + q.c, d + q.d
);
}
subtract(q) {
const { a, b, c, d } = this;
return new Quaternion(
a - q.a, b - q.b, c - q.c, d - q.d
);
}
multiply(q) {
const { a, b, c, d } = this;
return new Quaternion(
a * q.a - b * q.b - c * q.c - d * q.d,
a * q.b + b * q.a + c * q.d - d * q.c,
a * q.c - b * q.d + c * q.a + d * q.b,
a * q.d + b * q.c - c * q.b + d * q.a,
);
}
// p/q = p q^(-1)
// โ note: p q^(-1) !== q^(-1) p
divide(q) {
return this.multiply(q.inverse);
}
// kq
scale(k) {
const { a, b, c, d } = this;
return new Quaternion(k * a, k * b, k * c, k * d);
}
// p โข q
dot(q) {
const { a, b, c, d } = q;
return this.a * a + this.b * b + this.c * c + this.d * d;
}
// p x q = (p โข q) - bar(p) q
cross(q) {
return this.dot(q).asQuaternion.subtract(
this.conjugate.multiply(q)
);
}
// ---------------------------
// โญ helpers
// ---------------------------
// distance
distance(q) {
return this.subtract(q).norm;
}
// equal
equal(q, {threshold=1e-10}={}) {
return this.distance(q).equal(0, {threshold});
}
// toString
toString(places=3) {
const { a, b, c, d } = this;
return `(${a.round(places)},${b.round(places)},${c.round(places)},${d.round(places)})`;
}
}
// โญ CommonJS export
module.exports = {
Quaternion,
deg,
};
// export {deg}; // ES module export
'use strict'; // โญ toggle sloppy/strict mode
const { log } = console;
// โญ import
const _Number = require('./ext/Number+ext.js'); // Number+ext
// ---------------------------------------------------------------------------
const e = Number.EPSILON; // 2^(-52) (very small)
const BIG = Math.pow(10, 300); // 10^300 (very big)
// โ
log expressions that never throw
// ---------------------------------------------------------------------------
;[
// without prefix
(45).binary, // '101101'
(88).octal, // '130'
(123).hex, // '7B'
// with prefix
(123).toBinary(), // '0b1111011'
(123).toOctal(), // '0o173'
(123).toHex(), // '0x7B'
// with options
(123).toHex({pad: {length: 4}}), // '0x007B'
(123).toHex({pad: {char: '_'}}), // '0x7B' (lenth = 0, by default)
(123).toHex({pad: {char: '_', length: 6}}), // '0x____7B'
(123).toHex({prefix: '#', pad: {length: 3}}), // '#07B'
// equality
Number.EPSILON, // 2.220446049250313e-16 = 2^(-52)
(0.1 + 0.2) - 0.3, // 5.551115123125783e-17 < e
0.1 + 0.2 === 0.3, // โ๏ธ false (should be true)
(0.1 + 0.2).isEqualTo(0.3), // true
1.5 + e > 1.5, // true
2.5 + e > 2.5, // โ๏ธ false (should be true)
BIG + 1 > BIG, // โ๏ธ false (should be true)
BIG + 1 === BIG, // โ๏ธ true (should be false)
// test threshold
(0.1).isEqualTo(0.11, {threshold: 0.05}), // true
].forEach(x => log(x));
Vector - use
.isEqualTo()
.Canvas+ext - use
deg
.
parseInt() - global function
Number.parseInt() === parseInt()
Number.parseFloat() === parseFloat()
History
0 (โข) first version
1 (+) .clamp(), .isEqualTo()
2 (/) .isEqualTo() renamed to .equal()
3 (/) Number.EPSILON -> 1e-10
// ----------------------------------
// Number.prototype extension
// ----------------------------------
// ๐ธ n.isEven
// ๐ธ n.isOdd
// ๐ธ n.binary
// ๐ธ n.octal
// ๐ธ n.hex
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
// ๐น n.toOctal({prefix:, pad: {length:, char:})
// ๐น n.toHex({prefix:, pad: {length:, char:})
// โญ multiple of
Object.defineProperties(Number.prototype, {
// ๐ธ n.isEven
isEven: {
get() { return this % 2 === 0 },
},
// ๐ธ n.isOdd
isOdd: {
get() { return Math.abs(this % 2) === 1 },
},
// ๐ธ n.binary
binary: {
get() { return this.toString(2) },
},
// ๐ธ n.octal
octal: {
get() { return this.toString(8) },
},
// ๐ธ n.hex
hex: {
get() { return this.toString(16).toUpperCase() },
},
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
toBinary: {
value: formatFunc('0b', 2),
},
// ๐น n.toOctal({prefix:, pad: {length:, char:}})
toOctal: {
value: formatFunc('0o', 8),
},
// ๐น n.toHex({prefix:, pad: {length:, char:}})
toHex: {
value: formatFunc('0x', 16),
},
});
// โญ general function
function formatFunc(pre, base) {
return function format({
prefix = pre,
pad = { length: 0, char: '0' },
} = {}) {
// โญ `pad` might be overridden, better destructure again
const { length = 0, char = '0' } = pad;
return prefix +
this.toString(base).padStart(length, char).toUpperCase();
}
}
// CommonJS export
module.exports = {};
// 2023.01.20 - 11:47 (+) .clamp(), .isEqualTo
// -----------------------------------------------------
// โญ Number+ext
// -----------------------------------------------------
// (integer-related)
// ๐ธ n.isEven
// ๐ธ n.isOdd
// ๐ธ n.binary
// ๐ธ n.octal
// ๐ธ n.hex
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
// ๐น n.toOctal({prefix:, pad: {length:, char:})
// ๐น n.toHex({prefix:, pad: {length:, char:})
// -----------------------------------------------------
// ๐น .clamp()
// ๐น .isEqualTo()
//
Object.defineProperties(Number.prototype, {
// ---------------
// integer-related
// ---------------
// ๐ธ n.isEven
isEven: {
get() { return this % 2 === 0 },
},
// ๐ธ n.isOdd
isOdd: {
get() { return Math.abs(this % 2) === 1 },
},
// ๐ธ n.binary
binary: {
get() { return this.toString(2) },
},
// ๐ธ n.octal
octal: {
get() { return this.toString(8) },
},
// ๐ธ n.hex
hex: {
get() { return this.toString(16).toUpperCase() },
},
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
toBinary: {
value: formatFunc('0b', 2),
},
// ๐น n.toOctal({prefix:, pad: {length:, char:}})
toOctal: {
value: formatFunc('0o', 8),
},
// ๐น n.toHex({prefix:, pad: {length:, char:}})
toHex: {
value: formatFunc('0x', 16),
},
// ---------------
// range
// ---------------
// ๐น .clamp(min, max)
clamp: {
value: function(min, max){
if (min > max) [min, max] = [max, min];
return Math.min(max, Math.max(min, this));
},
},
// ๐น .isEqualTo()
isEqualTo: {
value: function(n, {threshold = Number.EPSILON}={}){
return areEqualNumbers(this, n, {threshold});
},
},
});
// โญ helpers
// -----------------------------------------------------
// general format function
function formatFunc(pre, base) {
return function format({
prefix = pre,
pad = { length: 0, char: '0' },
} = {}) {
// โญ `pad` might be overridden, better destructure again
const { length = 0, char = '0' } = pad;
return prefix +
this.toString(base).padStart(length, char).toUpperCase();
}
}
// areEqualNumbers(x, y {threshold})
function areEqualNumbers(x, y, {threshold = Number.EPSILON}={}) {
return Math.abs(x - y) < threshold;
}
// โญ export
module.exports = {}; // CommonJS export
// export default {}; // ES module export
// 2023.01.20 - 16:45 (+) deg
// 2023.01.20 - 11:47 (+) .clamp(), .isEqualTo (*first recorded)
// -----------------------------------------------------
// โญ Number+ext
// -----------------------------------------------------
// (constants)
// - deg // degree = PI/180
// -----------------------------------------------------
// (integer-related)
// ๐ธ n.isEven
// ๐ธ n.isOdd
// ๐ธ n.binary
// ๐ธ n.octal
// ๐ธ n.hex
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
// ๐น n.toOctal({prefix:, pad: {length:, char:})
// ๐น n.toHex({prefix:, pad: {length:, char:})
// -----------------------------------------------------
// ๐น .clamp()
// ๐น .isEqualTo()
//
Object.defineProperties(Number.prototype, {
// ---------------
// integer-related
// ---------------
// ๐ธ n.isEven
isEven: {
get() { return this % 2 === 0 },
},
// ๐ธ n.isOdd
isOdd: {
get() { return Math.abs(this % 2) === 1 },
},
// ๐ธ n.binary
binary: {
get() { return this.toString(2) },
},
// ๐ธ n.octal
octal: {
get() { return this.toString(8) },
},
// ๐ธ n.hex
hex: {
get() { return this.toString(16).toUpperCase() },
},
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
toBinary: {
value: formatFunc('0b', 2),
},
// ๐น n.toOctal({prefix:, pad: {length:, char:}})
toOctal: {
value: formatFunc('0o', 8),
},
// ๐น n.toHex({prefix:, pad: {length:, char:}})
toHex: {
value: formatFunc('0x', 16),
},
// ---------------
// range
// ---------------
// ๐น .clamp(min, max)
clamp: {
value: function(min, max){
if (min > max) [min, max] = [max, min];
return Math.min(max, Math.max(min, this));
},
},
// ๐น .isEqualTo()
isEqualTo: {
value: function(n, {threshold = Number.EPSILON}={}){
return areEqualNumbers(this, n, {threshold});
},
},
});
// โญ helpers
// -----------------------------------------------------
// general format function
function formatFunc(pre, base) {
return function format({
prefix = pre,
pad = { length: 0, char: '0' },
} = {}) {
// โญ `pad` might be overridden, better destructure again
const { length = 0, char = '0' } = pad;
return prefix +
this.toString(base).padStart(length, char).toUpperCase();
}
}
// areEqualNumbers(x, y {threshold})
function areEqualNumbers(x, y, {threshold = Number.EPSILON}={}) {
return Math.abs(x - y) < threshold;
}
// deg
const deg = Math.PI / 180;
// โญ export
module.exports = {deg}; // CommonJS export
// export {deg}; // ES module export
// 2023.03.01 - 13:30 (/) isEqualTo -> equal, Number.EPSILON -> 1e-10
// (-) areEqualNumbers()
// (+) .round()
// 2023.01.20 - 16:45 (+) deg
// 2023.01.20 - 11:47 (+) .clamp(), .isEqualTo (*first recorded)
// -----------------------------------------------------
const {abs} = Math;
const {log} = console;
// deg
const deg = Math.PI / 180;
// โญ Number+ext
// -----------------------------------------------------
// (constants)
// - deg // degree = PI/180
// -----------------------------------------------------
// (integer-related)
// ๐ธ n.isEven
// ๐ธ n.isOdd
// ๐ธ n.binary
// ๐ธ n.octal
// ๐ธ n.hex
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
// ๐น n.toOctal({prefix:, pad: {length:, char:})
// ๐น n.toHex({prefix:, pad: {length:, char:})
// -----------------------------------------------------
// ๐น .clamp()
// ๐น .equal()
//
Object.defineProperties(Number.prototype, {
// ---------------
// integer-related
// ---------------
// ๐ธ n.isEven
isEven: {
get() { return this % 2 === 0 },
},
// ๐ธ n.isOdd
isOdd: {
get() { return Math.abs(this % 2) === 1 },
},
// ๐ธ n.binary
binary: {
get() { return this.toString(2) },
},
// ๐ธ n.octal
octal: {
get() { return this.toString(8) },
},
// ๐ธ n.hex
hex: {
get() { return this.toString(16).toUpperCase() },
},
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
toBinary: {
value: formatFunc('0b', 2),
},
// ๐น n.toOctal({prefix:, pad: {length:, char:}})
toOctal: {
value: formatFunc('0o', 8),
},
// ๐น n.toHex({prefix:, pad: {length:, char:}})
toHex: {
value: formatFunc('0x', 16),
},
// ---------------
// range
// ---------------
// ๐น .clamp(min, max)
clamp: {
value: function(min, max){
if (min > max) [min, max] = [max, min];
return Math.min(max, Math.max(min, this));
},
},
// ๐น .equal()
equal: {
value: function(y, {threshold=1e-10}={}){
return abs(this - y) < threshold;
},
},
// ๐น .round()
round: {
value: function(places){
return +this.toFixed(places);
},
},
});
// โญ helpers
// -----------------------------------------------------
// general format function
function formatFunc(pre, base) {
return function format({
prefix = pre,
pad = { length: 0, char: '0' },
} = {}) {
// โญ `pad` might be overridden, better destructure again
const { length = 0, char = '0' } = pad;
return prefix +
this.toString(base).padStart(length, char).toUpperCase();
}
}
// โญ export
module.exports = {deg}; // CommonJS export
// export {deg}; // ES module export
Last updated