How to define an exabyte constant in TypeScript

1 Answer

0 votes
/**
 * TypeScript uses JavaScript's number type (IEEE‑754 double),
 * which can only safely represent integers up to 2^53 - 1.
 *
 * Exabyte sizes:
 *
 *   1 EiB = 2^60  = 1,152,921,504,606,846,976 bytes
 *   1 EB  = 10^18 = 1,000,000,000,000,000,000 bytes
 *
 * These exceed Number.MAX_SAFE_INTEGER, so we use BigInt.
 */

// Binary exabyte (exbibyte), using a BigInt bit shift.
// 1n << 60n = 2^60 bytes.
export const EXABYTE_EIB = 1n << 60n;

// Decimal exabyte (SI), using a readable BigInt literal.
export const EXABYTE_EB = 1_000_000_000_000_000_000n;

console.log("1 EiB =", EXABYTE_EIB, "bytes");
console.log("1 EB  =", EXABYTE_EB,  "bytes");



/*
run:

1 EiB = 1152921504606846976n bytes
1 EB  = 1000000000000000000n bytes

*/

 



answered May 3 by avibootz
...