elflib
Version:
ELF file reader and writer
54 lines (53 loc) • 1.32 kB
JavaScript
export function subtract(a, b) {
if (typeof a === 'bigint' && typeof b === 'bigint') {
return a - b;
}
else if (typeof a === 'number' && typeof b === 'number') {
return a - b;
}
else {
return BigInt(a) - BigInt(b);
}
}
export function add(a, b) {
if (typeof a === 'bigint' && typeof b === 'bigint') {
return a + b;
}
else if (typeof a === 'number' && typeof b === 'number') {
return a + b;
}
else {
return BigInt(a) + BigInt(b);
}
}
export function divide(a, b) {
if (typeof a === 'bigint' && typeof b === 'bigint') {
return a / b;
}
else if (typeof a === 'number' && typeof b === 'number') {
return parseInt((a / b));
}
else {
return BigInt(a) / BigInt(b);
}
}
const tooBigInt = BigInt(1e51);
export function toNumberSafe(a, warnings) {
if (typeof a === 'bigint') {
if (a > tooBigInt) {
if (warnings) {
warnings.push('BigInt Overflow');
}
else {
throw new Error('BigInt Overflow');
}
}
else {
return Number(a);
}
}
else if (typeof a === 'number') {
return a;
}
throw new Error('Invalid Input for BigInt conversion');
}