Skip to content

Instantly share code, notes, and snippets.

@zjkuang
Last active March 23, 2023 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zjkuang/c2c3710b90e50a71f290051e6ab210c0 to your computer and use it in GitHub Desktop.
Save zjkuang/c2c3710b90e50a71f290051e6ab210c0 to your computer and use it in GitHub Desktop.
Get the smallest positive number in JavaScript
// Consider the "smallest" positive number in JavaScript, (where there are 322 zeros before "1" after the point,)
const a = 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001;
// I call it "smallest" because if we add one more zero before the "1", JavaScript takes it as zero:
const a0 = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001;
console.log(`a0 == 0: ${a0 == 0}`); // true
// Now let's check a number b9,
// a = 0.0...01 // 0.(322 zeros)1
// b9 = 0.0...009 // 0.(322 zeros)09
const b9 = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009;
// Mathematically b9 < a, but JavaScript thinks b9 == a
console.log(`b9 < a: ${b9 < a}`); // false
console.log(`b9 == a: ${b9 == a}`); // true
// If we decrease b9 a little bit to b8 = 0.0...008, we would get the same result
const b8 = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008;
console.log(`b8 < a: ${b8 < a}`); // false
console.log(`b8 == a: ${b8 == a}`); // true
// And we decrease b8 to b7 = 0.0...007, now we could get the mathematically correct result
const b7 = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007;
console.log(`b7 < a: ${b7 < a}`); // true
console.log(`b7 == a: ${b7 == a}`); // false
// Now let's try another number between b7 and b8, starting from b79 = 0.0...0079
const b79 = 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000079;
console.log(`b79 < a: ${b79 < a}`); // false
console.log(`b79 == a: ${b79 == a}`); // true
// So b79 is "javascriptally" equal to b8,
// If we try b71 = 0.0...0071
const b71 = 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000071;
console.log(`b71 < a: ${b71 < a}`); // true
console.log(`b71 == a: ${b71 == a}`); // false
// Try the numbers between b71 and b79, we could get b74 (0.0...0074) and b75 (0.0...0075),
const b75 = 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000075;
const b74 = 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000074;
console.log(`b75 < a: ${b75 < a}`); // false
console.log(`b75 == a: ${b75 == a}`); // true
console.log(`b74 < a: ${b74 < a}`); // true
console.log(`b74 == a: ${b74 == a}`); // false
// Keep going this way, we could "try out" a series of b: 0.0...00741098...
// But does this series go on infinitely? The following piece of code would do this job for us.
////////////////////////////////////////////////////////////////////////////////
//
const az = Array(322).fill('0');
const zeros = az.join('');
const smallest = Number(`0.${zeros}1`); // 0.0...01 (322 zeros before "1" after the point)
const NumberOfDigits = 1000;
let countDigits = 0;
let seq = '';
while (true) {
let lastLess = 0;
for (let i = 0; i < 10; i++) {
const smaller = Number(`0.${zeros}0${seq}${i}`);
if (smaller < smallest) {
lastLess = i;
} else {
break;
}
}
seq = `${seq}${lastLess}`;
countDigits++;
if (countDigits >= NumberOfDigits) {
break;
}
}
console.log(seq);
// And the output is
// 74109846876186981626485318930233205854758970392148714663837852375101326090531312
// 77979497545424539885696948470431685765963899850655339096945981621940161728171894
// 51069785467106791768725751773473155533077954085498096084575009581113730347476580
// 96871009590975442271004757307809711118935784838675653998783503015228055934046593
// 73979179073872386829939581848166016912201945649993128979841136206248449867871357
// 21803522090170239032857917325202205289740208029068540216066123755499834026713000
// 35812486479041385743401875520901590172592547146296175134159774938718574737870961
// 64563890871811984127167305601704549300470526959016576377688490826798697257336652
// 17655679410725087643375608460039849049721491174630855395563541886415131684784363
// 13080237596295773983001708984374999999999999999999999999999999999999999999999999
// 99999999999999999999999999999999999999999999999999999999999999999999999999999999
// 99999999999999999999999999999999999999999999999999999999999999999999999999999999
// 9999999999999999999999999999999999999999
// Starting from position number 753, it repeats with all "9"s.
//
////////////////////////////////////////////////////////////////////////////////
const c49 = 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000741098468761869816264853189302332058547589703921487146638378523751013260905313127797949754542453988569694847043168576596389985065533909694598162194016172817189451069785467106791768725751773473155533077954085498096084575009581113730347476580968710095909754422710047573078097111189357848386756539987835030152280559340465937397917907387238682993958184816601691220194564999312897984113620624844986787135721803522090170239032857917325202205289740208029068540216066123755499834026713000358124864790413857434018755209015901725925471462961751341597749387185747378709616456389087181198412716730560170454930047052695901657637768849082679869725733665217655679410725087643375608460039849049721491174630855395563541886415131684784363130802375962957739830017089843749;
const c50 = 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000741098468761869816264853189302332058547589703921487146638378523751013260905313127797949754542453988569694847043168576596389985065533909694598162194016172817189451069785467106791768725751773473155533077954085498096084575009581113730347476580968710095909754422710047573078097111189357848386756539987835030152280559340465937397917907387238682993958184816601691220194564999312897984113620624844986787135721803522090170239032857917325202205289740208029068540216066123755499834026713000358124864790413857434018755209015901725925471462961751341597749387185747378709616456389087181198412716730560170454930047052695901657637768849082679869725733665217655679410725087643375608460039849049721491174630855395563541886415131684784363130802375962957739830017089843750;
console.log(`c49 < a: ${c49 < a}`); // true
console.log(`c49 == a: ${c49 == a}`); // false
console.log(`c50 < a: ${c50 < a}`); // false
console.log(`c50 == a: ${c50 == a}`); // true
// Thus, the smallest number in JavaScript is not that "a", but 0.0...0741098...8984374999...999...999...
// I am using a 64-bit CPU to get the above result. Not sure how it turns out to be on a 32-bit CPU, and future 128-bit CPU.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment