@@ -1,4 +1,4 @@
'use strict' ;
"use strict" ;
function round(number, precision = 3) {
function round(number, precision = 3) {
return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision);
return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision);
@@ -16,34 +16,40 @@ function numberRough(value, suffix = "") {
} else {
} else {
var scale = Math.floor(Math.log10(value));
var scale = Math.floor(Math.log10(value));
switch (scale) {
switch (scale) {
case 1: return "dozens " + suffix;
case 2: return "hundreds " + suffix;
case 1:
return "dozens " + suffix;
case 2:
return "hundreds " + suffix;
default:
default:
let prefix = "";
let prefix = "";
if (scale % 3 == 1)
prefix = "tens of ";
else if (scale % 3 == 2)
prefix = "hundreds of ";
if (scale % 3 == 1) prefix = "tens of ";
else if (scale % 3 == 2) prefix = "hundreds of ";
let order = Math.floor(scale / 3);
let order = Math.floor(scale / 3);
switch (order) {
switch (order) {
case 1: return prefix + "thousands " + suffix;
case 2: return prefix + "millions " + suffix;
case 3: return prefix + "billions " + suffix;
case 4: return prefix + "trillions " + suffix;
case 5: return prefix + "quadrillions " + suffix;
case 6: return prefix + "quintillions " + suffix;
default: return "uncountably many";
case 1:
return prefix + "thousands " + suffix;
case 2:
return prefix + "millions " + suffix;
case 3:
return prefix + "billions " + suffix;
case 4:
return prefix + "trillions " + suffix;
case 5:
return prefix + "quadrillions " + suffix;
case 6:
return prefix + "quintillions " + suffix;
default:
return "uncountably many";
}
}
}
}
}
}
}
}
function fixedIfDecimal(num, fixed) {
function fixedIfDecimal(num, fixed) {
if (fixed === undefined)
return num.toString();
if (fixed === undefined) return num.toString();
else;
else;
return num.toFixed(fixed);
return num.toFixed(fixed);
}
}
@@ -56,9 +62,12 @@ function number(value, type = "full", fixed) {
return fixedIfDecimal(val, fixed);
return fixedIfDecimal(val, fixed);
}
}
case "scientific": return val.toExponential(3, fixed).toString();
case "words": return number_words_repeated(val, fixed);
case "prefix": return number_prefix(val, fixed);
case "scientific":
return val.toExponential(3, fixed).toString();
case "words":
return number_words_repeated(val, fixed);
case "prefix":
return number_prefix(val, fixed);
}
}
}
}
@@ -68,108 +77,169 @@ function number_words(value) {
return fixedIfDecimal(value, fixed);
return fixedIfDecimal(value, fixed);
}
}
switch (scale) {
switch (scale) {
case 0: return value.toString();
case 1: return Math.round(value / 1e3).toString() + " thousand";
case 2: return Math.round(value / 1e6).toString() + " million";
case 3: return Math.round(value / 1e9).toString() + " billion";
case 4: return Math.round(value / 1e12).toString() + " trillion";
case 5: return Math.round(value / 1e15).toString() + " quadrillion";
case 6: return Math.round(value / 1e18).toString() + " quintillion";
case 7: return Math.round(value / 1e21).toString() + " sextillion";
case 8: return Math.round(value / 1e24).toString() + " septillion";
case 9: return Math.round(value / 1e27).toString() + " octillion";
case 10: return Math.round(value / 1e30).toString() + " nonillion";
case 11: return Math.round(value / 1e33).toString() + " decillion";
case 12: return Math.round(value / 1e36).toString() + " undecillion";
case 13: return Math.round(value / 1e39).toString() + " duodecillion";
case 14: return Math.round(value / 1e42).toString() + " tredecillion";
case 15: return Math.round(value / 1e45).toString() + " quattuordecillion";
case 16: return Math.round(value / 1e48).toString() + " quindecillion";
case 17: return Math.round(value / 1e51).toString() + " sexdecillion";
case 18: return Math.round(value / 1e54).toString() + " septendecillion";
case 19: return Math.round(value / 1e57).toString() + " octodecillion";
case 20: return Math.round(value / 1e60).toString() + " novemdecillion";
default: return Math.round(value / 1e63).toString() + " vigintillion";
case 0:
return value.toString();
case 1:
return Math.round(value / 1e3).toString() + " thousand";
case 2:
return Math.round(value / 1e6).toString() + " million";
case 3:
return Math.round(value / 1e9).toString() + " billion";
case 4:
return Math.round(value / 1e12).toString() + " trillion";
case 5:
return Math.round(value / 1e15).toString() + " quadrillion";
case 6:
return Math.round(value / 1e18).toString() + " quintillion";
case 7:
return Math.round(value / 1e21).toString() + " sextillion";
case 8:
return Math.round(value / 1e24).toString() + " septillion";
case 9:
return Math.round(value / 1e27).toString() + " octillion";
case 10:
return Math.round(value / 1e30).toString() + " nonillion";
case 11:
return Math.round(value / 1e33).toString() + " decillion";
case 12:
return Math.round(value / 1e36).toString() + " undecillion";
case 13:
return Math.round(value / 1e39).toString() + " duodecillion";
case 14:
return Math.round(value / 1e42).toString() + " tredecillion";
case 15:
return Math.round(value / 1e45).toString() + " quattuordecillion";
case 16:
return Math.round(value / 1e48).toString() + " quindecillion";
case 17:
return Math.round(value / 1e51).toString() + " sexdecillion";
case 18:
return Math.round(value / 1e54).toString() + " septendecillion";
case 19:
return Math.round(value / 1e57).toString() + " octodecillion";
case 20:
return Math.round(value / 1e60).toString() + " novemdecillion";
default:
return Math.round(value / 1e63).toString() + " vigintillion";
}
}
}
}
function number_words_repeated(value, fixed) {
function number_words_repeated(value, fixed) {
if (value == Infinity)
return "a lot of";
if (value == Infinity) return "a lot of";
var scale = Math.floor(Math.log(value) / Math.log(1000));
var scale = Math.floor(Math.log(value) / Math.log(1000));
if (scale < 0)
return fixedIfDecimal(value, fixed);
if (scale < 0) return fixedIfDecimal(value, fixed);
switch (scale) {
switch (scale) {
case 0: return fixedIfDecimal(value, fixed);
case 1: return Math.round(value / 1e3).toString() + " thousand";
case 2: return Math.round(value / 1e6).toString() + " million";
case 3: return Math.round(value / 1e9).toString() + " billion";
case 4: return Math.round(value / 1e12).toString() + " trillion";
case 5: return Math.round(value / 1e15).toString() + " quadrillion";
case 6: return Math.round(value / 1e18).toString() + " quintillion";
case 7: return Math.round(value / 1e21).toString() + " sextillion";
case 8: return Math.round(value / 1e24).toString() + " septillion";
case 9: return Math.round(value / 1e27).toString() + " octillion";
case 10: return Math.round(value / 1e30).toString() + " nonillion";
case 11: return Math.round(value / 1e33).toString() + " decillion";
default: return number_words_repeated(value / 1e33) + " decillion";
case 0:
return fixedIfDecimal(value, fixed);
case 1:
return Math.round(value / 1e3).toString() + " thousand";
case 2:
return Math.round(value / 1e6).toString() + " million";
case 3:
return Math.round(value / 1e9).toString() + " billion";
case 4:
return Math.round(value / 1e12).toString() + " trillion";
case 5:
return Math.round(value / 1e15).toString() + " quadrillion";
case 6:
return Math.round(value / 1e18).toString() + " quintillion";
case 7:
return Math.round(value / 1e21).toString() + " sextillion";
case 8:
return Math.round(value / 1e24).toString() + " septillion";
case 9:
return Math.round(value / 1e27).toString() + " octillion";
case 10:
return Math.round(value / 1e30).toString() + " nonillion";
case 11:
return Math.round(value / 1e33).toString() + " decillion";
default:
return number_words_repeated(value / 1e33) + " decillion";
}
}
}
}
function number_prefix(value, fixed) {
function number_prefix(value, fixed) {
var scale = Math.floor(Math.log(value) / Math.log(1000));
var scale = Math.floor(Math.log(value) / Math.log(1000));
if (scale < 0)
return fixedIfDecimal(value, fixed);
if (scale < 0) return fixedIfDecimal(value, fixed);
switch (scale) {
switch (scale) {
case 0: return fixedIfDecimal(value, fixed);
case 1: return Math.round(value / 1e3).toString() + "K";
case 2: return Math.round(value / 1e6).toString() + "M";
case 3: return Math.round(value / 1e9).toString() + "G";
case 4: return Math.round(value / 1e12).toString() + "T";
case 5: return Math.round(value / 1e15).toString() + "P";
case 6: return Math.round(value / 1e18).toString() + "E";
case 7: return Math.round(value / 1e21).toString() + "Z";
default: return Math.round(value / 1e24).toString() + "Y";
case 0:
return fixedIfDecimal(value, fixed);
case 1:
return Math.round(value / 1e3).toString() + "K";
case 2:
return Math.round(value / 1e6).toString() + "M";
case 3:
return Math.round(value / 1e9).toString() + "G";
case 4:
return Math.round(value / 1e12).toString() + "T";
case 5:
return Math.round(value / 1e15).toString() + "P";
case 6:
return Math.round(value / 1e18).toString() + "E";
case 7:
return Math.round(value / 1e21).toString() + "Z";
default:
return Math.round(value / 1e24).toString() + "Y";
}
}
}
}
function mass(kg, type = "metric", singular = false) {
function mass(kg, type = "metric", singular = false) {
switch (type) {
switch (type) {
case "metric": return metricMass(kg, singular);
case "SI": return metricSymMass(kg, singular);
case "customary": return customaryMass(kg, singular);
case "US": return customarySymMass(kg, singular);
case "approx": return approxMass(kg, singular);
case "metric":
return metricMass(kg, singular);
case "SI":
return metricSymMass(kg, singular);
case "customary":
return customaryMass(kg, singular);
case "US":
return customarySymMass(kg, singular);
case "approx":
return approxMass(kg, singular);
}
}
}
}
function length(m, type = "metric", singular = false) {
function length(m, type = "metric", singular = false) {
switch (type) {
switch (type) {
case "metric": return metricLength(m, singular);
case "SI": return metricSymLength(m, singular);
case "customary": return customaryLength(m, singular);
case "US": return customarySymLength(m, singular);
case "approx": return approxLength(m, singular);
case "metric":
return metricLength(m, singular);
case "SI":
return metricSymLength(m, singular);
case "customary":
return customaryLength(m, singular);
case "US":
return customarySymLength(m, singular);
case "approx":
return approxLength(m, singular);
}
}
}
}
function area(m2, type = "metric", singular = false) {
function area(m2, type = "metric", singular = false) {
switch (type) {
switch (type) {
case "metric": return metricArea(m2, singular);
case "SI": return metricSymArea(m2, singular);
case "customary": return customaryArea(m2, singular);
case "US": return customarySymArea(m2, singular);
case "approx": return approxArea(m2, singular);
case "metric":
return metricArea(m2, singular);
case "SI":
return metricSymArea(m2, singular);
case "customary":
return customaryArea(m2, singular);
case "US":
return customarySymArea(m2, singular);
case "approx":
return approxArea(m2, singular);
}
}
}
}
function volume(m3, type = "metric", singular = false) {
function volume(m3, type = "metric", singular = false) {
switch (type) {
switch (type) {
case "metric": return metricVolume(m3, singular);
case "SI": return metricSymVolume(m3, singular);
case "customary": return customaryVolume(m3, singular);
case "US": return customarySymVolume(m3, singular);
case "approx": return approxVolume(m3, singular);
case "metric":
return metricVolume(m3, singular);
case "SI":
return metricSymVolume(m3, singular);
case "customary":
return customaryVolume(m3, singular);
case "US":
return customarySymVolume(m3, singular);
case "approx":
return approxVolume(m3, singular);
}
}
}
}
@@ -266,13 +336,21 @@ function approxMass(kg, singular = false) {
return mass + (singular || mass == 1 ? " tank" : " tanks");
return mass + (singular || mass == 1 ? " tank" : " tanks");
} else if (kg < 5.2e10) {
} else if (kg < 5.2e10) {
let mass = round(kg / 9.7e7, 2);
let mass = round(kg / 9.7e7, 2);
return mass + (singular || mass == 1 ? " aircraft carrier" : " aircraft carriers");
return (
mass +
(singular || mass == 1 ? " aircraft carrier" : " aircraft carriers")
);
} else if (kg < 1.5e13) {
} else if (kg < 1.5e13) {
let mass = round(kg / 5.2e10, 2);
let mass = round(kg / 5.2e10, 2);
return mass + (singular || mass == 1 ? " Great Wall of China" : " Great Wall Of Chinas");
return (
mass +
(singular || mass == 1 ? " Great Wall of China" : " Great Wall Of Chinas")
);
} else if (kg < 5e21) {
} else if (kg < 5e21) {
let mass = round(kg / 1.5e15, 2);
let mass = round(kg / 1.5e15, 2);
return mass + (singular || mass == 1 ? " New York City" : " New York Cities");
return (
mass + (singular || mass == 1 ? " New York City" : " New York Cities")
);
//this figure includes a lot of underlying bedrock, just the city itself is 1.13587210581190e11 but I needed a good figure to fit in this spot
//this figure includes a lot of underlying bedrock, just the city itself is 1.13587210581190e11 but I needed a good figure to fit in this spot
} else if (kg < 6e23) {
} else if (kg < 6e23) {
let mass = round(kg / 4.6e20, 2);
let mass = round(kg / 4.6e20, 2);
@@ -352,7 +430,7 @@ function customarySymLength(m, singular = false) {
if (ft < 1) {
if (ft < 1) {
let length = round(ft * 12, 0);
let length = round(ft * 12, 0);
return length + "\"" ;
return length + '"' ;
} else if (ft < 5280) {
} else if (ft < 5280) {
let end = customarySymLength((ft - Math.floor(ft)) / 3.28084, singular);
let end = customarySymLength((ft - Math.floor(ft)) / 3.28084, singular);
let length = Math.floor(ft);
let length = Math.floor(ft);
@@ -369,13 +447,23 @@ function approxLength(m, singular = false) {
return length + (singular || length == 1 ? " person" : " people");
return length + (singular || length == 1 ? " person" : " people");
} else if (m < 350) {
} else if (m < 350) {
let length = round(m / 49, 1);
let length = round(m / 49, 1);
return length + (singular || length == 1 ? " football field" : " football fields");
return (
length +
(singular || length == 1 ? " football field" : " football fields")
);
} else if (m < 20000) {
} else if (m < 20000) {
let length = round(m / 449, 1);
let length = round(m / 449, 1);
return length + (singular || length == 1 ? " Empire State Building" : " Empire State Buildings");
return (
length +
(singular || length == 1
? " Empire State Building"
: " Empire State Buildings")
);
} else if (m < 2000000) {
} else if (m < 2000000) {
let length = round(m / 80467.2, 1);
let length = round(m / 80467.2, 1);
return length + (singular || length == 1 ? " Panama Canal" : " Panama Canals");
return (
length + (singular || length == 1 ? " Panama Canal" : " Panama Canals")
);
} else if (m < 3474574 * 2) {
} else if (m < 3474574 * 2) {
let length = round(m / 3474574, 1);
let length = round(m / 3474574, 1);
return length + (singular || length == 1 ? " Moon" : " moons");
return length + (singular || length == 1 ? " Moon" : " moons");
@@ -409,7 +497,10 @@ function approxLength(m, singular = false) {
function metricArea(m2, singular = false) {
function metricArea(m2, singular = false) {
if (m2 < 1 / 10) {
if (m2 < 1 / 10) {
let area = round(m2 * 10000, 2);
let area = round(m2 * 10000, 2);
return area + (singular || area == 1 ? " square centimeter" : " square centimeters");
return (
area +
(singular || area == 1 ? " square centimeter" : " square centimeters")
);
} else if (m2 < 100000) {
} else if (m2 < 100000) {
let area = round(m2, 2);
let area = round(m2, 2);
return area + (singular || area == 1 ? " square meter" : " square meters");
return area + (singular || area == 1 ? " square meter" : " square meters");
@@ -438,7 +529,7 @@ function customaryArea(m2, singular = false) {
if (ft2 < 1) {
if (ft2 < 1) {
let area = round(ft2 * 144, 0);
let area = round(ft2 * 144, 0);
return area + (singular || area == 1 ? " square inch" : " square inches");
return area + (singular || area == 1 ? " square inch" : " square inches");
} else if (ft2 < 5280 * 5280 / 10) {
} else if (ft2 < ( 5280 * 5280) / 10) {
let area = round(ft2, 1);
let area = round(ft2, 1);
return area + (singular || area == 1 ? " square foot" : " square feet");
return area + (singular || area == 1 ? " square foot" : " square feet");
} else {
} else {
@@ -463,21 +554,23 @@ function customarySymArea(m2, singular = false) {
function approxArea(m2, singular = false) {
function approxArea(m2, singular = false) {
if (m2 < 20000) {
if (m2 < 20000) {
let area = round(m2 / 5341.85, 1);
let area = round(m2 / 5341.85, 1);
return area + (singular || area == 1 ? " football field" : " football fields");
} else if (m2 < 9.36e+15) {
return (
area + (singular || area == 1 ? " football field" : " football fields")
);
} else if (m2 < 9.36e15) {
let area = round(m2 / 10117.1, 1);
let area = round(m2 / 10117.1, 1);
return area + (singular || area == 1 ? " block" : " blocks");
return area + (singular || area == 1 ? " block" : " blocks");
} else if (m2 < 3.7920361e+ 13) {
let area = round(m2 / 9.36e+ 8, 1);
} else if (m2 < 3.7920361e13) {
let area = round(m2 / 9.36e8, 1);
return area + (singular || area == 1 ? " city" : " cities");
return area + (singular || area == 1 ? " city" : " cities");
} else if (m2 < 9.4800902e+ 18) {
let area = round(m2 / 9.4800902e+ 12, 1);
} else if (m2 < 9.4800902e18) {
let area = round(m2 / 9.4800902e12, 1);
return area + (singular || area == 1 ? " moon" : " moons");
return area + (singular || area == 1 ? " moon" : " moons");
} else if (m2 < 2.8118957330513e+ 42) {
let area = round(m2 / 6.4900004e+ 28, 1);
} else if (m2 < 2.8118957330513e42) {
let area = round(m2 / 6.4900004e28, 1);
return area + (singular || area == 1 ? " solar system" : " solar systems");
return area + (singular || area == 1 ? " solar system" : " solar systems");
} else {
} else {
let area = round(m2 / 2.8118957330513e+ 42, 1);
let area = round(m2 / 2.8118957330513e42, 1);
return area + (singular || area == 1 ? " milky way" : " milky ways");
return area + (singular || area == 1 ? " milky way" : " milky ways");
}
}
}
}
@@ -491,13 +584,21 @@ function metricVolume(m3, singular = false) {
return volume + (singular || volume == 1 ? " liter" : " liters");
return volume + (singular || volume == 1 ? " liter" : " liters");
} else if (m3 < 1000000) {
} else if (m3 < 1000000) {
let volume = round(m3, 0);
let volume = round(m3, 0);
return volume + (singular || volume == 1 ? " cubic meter" : " cubic meters");
return (
volume + (singular || volume == 1 ? " cubic meter" : " cubic meters")
);
} else if (m3 < 1e12) {
} else if (m3 < 1e12) {
let volume = round(m3 / 1e9, 3);
let volume = round(m3 / 1e9, 3);
return volume + (singular || volume == 1 ? " cubic kilometer" : " cubic kilometers");
return (
volume +
(singular || volume == 1 ? " cubic kilometer" : " cubic kilometers")
);
} else {
} else {
let volume = round(m3 / 1e9, 0);
let volume = round(m3 / 1e9, 0);
return volume + (singular || volume == 1 ? " cubic kilometer" : " cubic kilometers");
return (
volume +
(singular || volume == 1 ? " cubic kilometer" : " cubic kilometers")
);
}
}
}
}
@@ -524,7 +625,9 @@ function customaryVolume(m3, singular = false) {
let gallons = m3 * 264.172;
let gallons = m3 * 264.172;
if (gallons < 1 / 16) {
if (gallons < 1 / 16) {
let volume = round(gallons * 128, 0);
let volume = round(gallons * 128, 0);
return volume + (singular || volume == 1 ? " fluid ounce" : " fluid ounces");
return (
volume + (singular || volume == 1 ? " fluid ounce" : " fluid ounces")
);
} else if (gallons < 1 / 4) {
} else if (gallons < 1 / 4) {
let volume = round(gallons * 16, 1);
let volume = round(gallons * 16, 1);
return volume + (singular || volume == 1 ? " cup" : " cups");
return volume + (singular || volume == 1 ? " cup" : " cups");
@@ -570,7 +673,7 @@ function approxVolume(m3, singular = false) {
if (m3 < 2 / 10000) {
if (m3 < 2 / 10000) {
let volume = round(m3 * 4e5, 0);
let volume = round(m3 * 4e5, 0);
return volume + (singular || volume == 1 ? " shot" : " shots");
return volume + (singular || volume == 1 ? " shot" : " shots");
} else if (m3 < .1) {
} else if (m3 < 0 .1) {
let volume = round(m3 * 2254, 1);
let volume = round(m3 * 2254, 1);
return volume + (singular || volume == 1 ? " glass" : " glasses");
return volume + (singular || volume == 1 ? " glass" : " glasses");
} else if (m3 < 100) {
} else if (m3 < 100) {
@@ -578,13 +681,21 @@ function approxVolume(m3, singular = false) {
return volume + (singular || volume == 1 ? " bathtub" : " bathtubs");
return volume + (singular || volume == 1 ? " bathtub" : " bathtubs");
} else if (m3 < 1e5) {
} else if (m3 < 1e5) {
let volume = round(m3 / 1000, 2);
let volume = round(m3 / 1000, 2);
return volume + (singular || volume == 1 ? " Olympic swimming pool" : " Olympic swimming pools");
return (
volume +
(singular || volume == 1
? " Olympic swimming pool"
: " Olympic swimming pools")
);
} else if (m3 < 1e9) {
} else if (m3 < 1e9) {
let volume = round(m3 / 3.2e5, 2);
let volume = round(m3 / 3.2e5, 2);
return volume + (singular || volume == 1 ? " oil tanker" : " oil tankers");
return volume + (singular || volume == 1 ? " oil tanker" : " oil tankers");
} else if (m3 < 1e15) {
} else if (m3 < 1e15) {
let volume = round(m3 / 1.8919e10, 3);
let volume = round(m3 / 1.8919e10, 3);
return volume + (singular || volume == 1 ? " Great Salt Lake" : " Great Salt Lakes");
return (
volume +
(singular || volume == 1 ? " Great Salt Lake" : " Great Salt Lakes")
);
} else if (m3 < 1e20) {
} else if (m3 < 1e20) {
let volume = round(m3 / 3.547e17, 3);
let volume = round(m3 / 3.547e17, 3);
return volume + (singular || volume == 1 ? " ocean" : " oceans");
return volume + (singular || volume == 1 ? " ocean" : " oceans");
@@ -597,17 +708,16 @@ function approxVolume(m3, singular = false) {
}
}
}
}
function makeSphere(input = 0, diameter = false) {
function makeSphere(input = 0, diameter = false) {
if (diameter = true) {
if (( diameter = true) ) {
input = input / 2;
input = input / 2;
}
}
return (4 / 3) * Math.PI * ( Math.pow(input, 3) );
return (4 / 3) * Math.PI * Math.pow(input, 3);
}
}
function breakSphere(input = 0, diameter = false) {
function breakSphere(input = 0, diameter = false) {
let output = math.pow((3 * input) / (4 * Math.PI), 1 / 3)
if (diameter = true) {
let output = math.pow((3 * input) / (4 * Math.PI), 1 / 3);
if (( diameter = true) ) {
output = output * 2;
output = output * 2;
}
}
return output;
return output;