big steppy
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

576 satır
20 KiB

  1. 'use strict';
  2. function round(number,precision=3) {
  3. return Math.round(number*Math.pow(10,precision)) / Math.pow(10,precision);
  4. }
  5. function numberRough(value,suffix="") {
  6. if (value == 1) {
  7. return "a single";
  8. } else if (value < 5) {
  9. return "a few";
  10. } else if (value < 12) {
  11. return "a handful " + suffix;
  12. } else if (value == 12) {
  13. return "a dozen";
  14. } else {
  15. var scale = Math.floor(Math.log10(value));
  16. switch(scale) {
  17. case 1: return "dozens " + suffix;
  18. case 2: return "hundreds " + suffix;
  19. default:
  20. let prefix = "";
  21. if (scale % 3 == 1)
  22. prefix = "tens of ";
  23. else if (scale % 3 == 2)
  24. prefix = "hundreds of ";
  25. let order = Math.floor(scale/3);
  26. switch(order) {
  27. case 1: return prefix + "thousands " + suffix;
  28. case 2: return prefix + "millions " + suffix;
  29. case 3: return prefix + "billions " + suffix;
  30. case 4: return prefix + "trillions " + suffix;
  31. case 5: return prefix + "quadrillions " + suffix;
  32. case 6: return prefix + "quintillions " + suffix;
  33. default: return "uncountably many";
  34. }
  35. }
  36. }
  37. }
  38. function fixedIfDecimal(num, fixed) {
  39. if (fixed === undefined)
  40. return num.toString();
  41. else;
  42. return num.toFixed(fixed);
  43. }
  44. function number(value, type="full", fixed) {
  45. console.log(value)
  46. var val = parseFloat(value);
  47. switch(type) {
  48. case "full":
  49. if (Math.log(value) / Math.log(10) < 10) {
  50. return fixedIfDecimal(val, fixed);
  51. }
  52. case "scientific": return val.toExponential(3, fixed).toString();
  53. case "words": return number_words_repeated(val, fixed);
  54. case "prefix": return number_prefix(val, fixed);
  55. }
  56. }
  57. function number_words(value) {
  58. var scale = Math.floor(Math.log(value) / Math.log(1000));
  59. if (scale < 0) {
  60. return fixedIfDecimal(value, fixed);
  61. }
  62. switch(scale) {
  63. case 0: return value.toString();
  64. case 1: return Math.round(value / 1e3).toString() + " thousand";
  65. case 2: return Math.round(value / 1e6).toString() + " million";
  66. case 3: return Math.round(value / 1e9).toString() + " billion";
  67. case 4: return Math.round(value / 1e12).toString() + " trillion";
  68. case 5: return Math.round(value / 1e15).toString() + " quadrillion";
  69. case 6: return Math.round(value / 1e18).toString() + " quintillion";
  70. case 7: return Math.round(value / 1e21).toString() + " sextillion";
  71. case 8: return Math.round(value / 1e24).toString() + " septillion";
  72. case 9: return Math.round(value / 1e27).toString() + " octillion";
  73. case 10: return Math.round(value / 1e30).toString() + " nonillion";
  74. case 11: return Math.round(value / 1e33).toString() + " decillion";
  75. case 12: return Math.round(value / 1e36).toString() + " undecillion";
  76. case 13: return Math.round(value / 1e39).toString() + " duodecillion";
  77. case 14: return Math.round(value / 1e42).toString() + " tredecillion";
  78. case 15: return Math.round(value / 1e45).toString() + " quattuordecillion";
  79. case 16: return Math.round(value / 1e48).toString() + " quindecillion";
  80. case 17: return Math.round(value / 1e51).toString() + " sexdecillion";
  81. case 18: return Math.round(value / 1e54).toString() + " septendecillion";
  82. case 19: return Math.round(value / 1e57).toString() + " octodecillion";
  83. case 20: return Math.round(value / 1e60).toString() + " novemdecillion";
  84. default: return Math.round(value / 1e63).toString() + " vigintillion";
  85. }
  86. }
  87. function number_words_repeated(value, fixed) {
  88. if (value == Infinity)
  89. return "a lot of";
  90. var scale = Math.floor(Math.log(value) / Math.log(1000));
  91. if (scale < 0)
  92. return fixedIfDecimal(value, fixed);
  93. switch(scale) {
  94. case 0: return fixedIfDecimal(value, fixed);
  95. case 1: return Math.round(value / 1e3).toString() + " thousand";
  96. case 2: return Math.round(value / 1e6).toString() + " million";
  97. case 3: return Math.round(value / 1e9).toString() + " billion";
  98. case 4: return Math.round(value / 1e12).toString() + " trillion";
  99. case 5: return Math.round(value / 1e15).toString() + " quadrillion";
  100. case 6: return Math.round(value / 1e18).toString() + " quintillion";
  101. case 7: return Math.round(value / 1e21).toString() + " sextillion";
  102. case 8: return Math.round(value / 1e24).toString() + " septillion";
  103. case 9: return Math.round(value / 1e27).toString() + " octillion";
  104. case 10: return Math.round(value / 1e30).toString() + " nonillion";
  105. case 11: return Math.round(value / 1e33).toString() + " decillion";
  106. default: return number_words_repeated(value / 1e33) + " decillion";
  107. }
  108. }
  109. function number_prefix(value, fixed) {
  110. var scale = Math.floor(Math.log(value) / Math.log(1000));
  111. if (scale < 0)
  112. return fixedIfDecimal(value, fixed);
  113. switch(scale) {
  114. case 0: return fixedIfDecimal(value, fixed);
  115. case 1: return Math.round(value / 1e3).toString() + "K";
  116. case 2: return Math.round(value / 1e6).toString() + "M";
  117. case 3: return Math.round(value / 1e9).toString() + "G";
  118. case 4: return Math.round(value / 1e12).toString() + "T";
  119. case 5: return Math.round(value / 1e15).toString() + "P";
  120. case 6: return Math.round(value / 1e18).toString() + "E";
  121. case 7: return Math.round(value / 1e21).toString() + "Z";
  122. default: return Math.round(value / 1e24).toString() + "Y";
  123. }
  124. }
  125. function mass(kg, type="metric", singular=false) {
  126. switch(type) {
  127. case "metric": return metricMass(kg, singular);
  128. case "SI": return metricSymMass(kg, singular);
  129. case "customary": return customaryMass(kg, singular);
  130. case "US": return customarySymMass(kg, singular);
  131. case "approx": return approxMass(kg, singular);
  132. }
  133. }
  134. function length(m, type="metric", singular=false) {
  135. switch(type) {
  136. case "metric": return metricLength(m, singular);
  137. case "SI": return metricSymLength(m, singular);
  138. case "customary": return customaryLength(m, singular);
  139. case "US": return customarySymLength(m, singular);
  140. case "approx": return approxLength(m, singular);
  141. }
  142. }
  143. function area(m2, type="metric", singular=false) {
  144. switch(type) {
  145. case "metric": return metricArea(m2, singular);
  146. case "SI": return metricSymArea(m2, singular);
  147. case "customary": return customaryArea(m2, singular);
  148. case "US": return customarySymArea(m2, singular);
  149. case "approx": return approxArea(m2, singular);
  150. }
  151. }
  152. function volume(m3, type="metric", singular=false) {
  153. switch(type) {
  154. case "metric": return metricVolume(m3, singular);
  155. case "SI": return metricSymVolume(m3, singular);
  156. case "customary": return customaryVolume(m3, singular);
  157. case "US": return customarySymVolume(m3, singular);
  158. case "approx": return approxVolume(m3, singular);
  159. }
  160. }
  161. function metricMass(kg, singular=false) {
  162. if (kg < 1/1000) {
  163. let mass = round(kg * 1e6,0);
  164. return mass + (singular || mass == 1 ? " milligram" : " milligrams");
  165. } else if (kg < 1) {
  166. let mass = round(kg * 1000,0);
  167. return mass + (singular || mass == 1 ? " gram" : " grams");
  168. } else if (kg < 5000) {
  169. let mass = round(kg,0);
  170. return mass + (singular || mass == 1 ? " kilogram" : " kilograms");
  171. } else if (kg < 5000000) {
  172. let mass = round(kg / 1000,1);
  173. return mass + (singular || mass == 1 ? " metric ton" : " metric tons");
  174. } else if (kg < 5000000000) {
  175. let mass = round(kg / 1000000,1);
  176. return mass + (singular || mass == 1 ? " kiloton" : " kilotons");
  177. } else if (kg < 5000000000000) {
  178. let mass = round(kg / 1000000000,1);
  179. return mass + (singular || mass == 1 ? " megaton" : " megatons");
  180. } else {
  181. let mass = round(kg / 1000000000000,1);
  182. return mass + (singular || mass == 1 ? " gigaton" : " gigatons");
  183. }
  184. }
  185. function metricSymMass(kg, singular=false) {
  186. if (kg < 1/1000) {
  187. let mass = round(kg * 1e6,0);
  188. return mass + " mg";
  189. } else if (kg < 1) {
  190. let mass = round(kg * 1000,0);
  191. return mass + " g";
  192. } else if (kg < 5000) {
  193. let mass = round(kg,0);
  194. return mass + " kg";
  195. } else if (kg < 5000000) {
  196. let mass = round(kg / 1000,1);
  197. return mass + " t";
  198. } else if (kg < 5000000000) {
  199. let mass = round(kg / 1000000,1);
  200. return mass + " kt";
  201. } else if (kg < 5000000000000) {
  202. let mass = round(kg / 1000000000,1);
  203. return mass + " mt";
  204. } else {
  205. let mass = round(kg / 1000000000000,1);
  206. return mass + " gt";
  207. }
  208. }
  209. function customaryMass(kg, singular=false) {
  210. let lbs = kg * 2.2;
  211. if (lbs < 1) {
  212. let mass = round(lbs * 16,0);
  213. return mass + (singular || mass == 1 ? " ounce" : " ounces");
  214. } else if (lbs < 2000) {
  215. let mass = round(lbs,0);
  216. return mass + (singular || mass == 1 ? " pound" : " pounds");
  217. } else {
  218. let mass = round(lbs / 2000,1);
  219. return mass + (singular || mass == 1 ? " ton" : " tons");
  220. }
  221. }
  222. function customarySymMass(kg, singular=false) {
  223. let lbs = kg * 2.2;
  224. if (lbs < 1) {
  225. let mass = round(lbs * 16,0);
  226. return mass + " oz";
  227. } else if (lbs < 2000) {
  228. let mass = round(lbs,0);
  229. return mass + (singular || mass == 1 ? " lb" : " lbs");
  230. } else {
  231. let mass = round(lbs / 2000,1);
  232. return mass + (singular || mass == 1 ? " ton" : " tons");
  233. }
  234. }
  235. function approxMass(kg, singular=false) {
  236. if (kg < 4500) {
  237. let mass = round(kg/1000,2);
  238. return mass + (singular || mass == 1 ? "car" : " cars");
  239. } else if (kg < 54431) {
  240. let mass = round(kg/6000,2);
  241. return mass + (singular || mass == 1 ? " elephant" : " elephants");
  242. //this unit almost never gets used and is mostly redundant, perhaps remove it if units are cleaned up
  243. } else if (kg < 10000000) {
  244. let mass = round(kg/54431.1,2);
  245. return mass + (singular || mass == 1 ? " tank" : " tanks");
  246. } else if (kg < 5.2e10) {
  247. let mass = round(kg/9.7e7,2);
  248. return mass + (singular || mass == 1 ? " aircraft carrier" : " aircraft carriers");
  249. } else if (kg < 1.5e13) {
  250. let mass = round(kg/5.2e10,2);
  251. return mass + (singular || mass == 1 ? " Great Wall of China" : " Great Wall Of Chinas");
  252. } else if (kg < 5e21) {
  253. let mass = round(kg/1.5e15,2);
  254. return mass + (singular || mass == 1 ? " New York City" : " New York Cities");
  255. //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
  256. } else if (kg < 6e23) {
  257. let mass = round(kg/4.6e20,2);
  258. return mass + (singular || mass == 1 ? " Australia" : " Australias");
  259. //this is a napkin math number based on the land area of Australia, 25km of height, and rough density of rock
  260. } else if (kg < 2e27) {
  261. let mass = round(kg/6e24,2);
  262. return mass + (singular || mass == 1 ? " Earth" :" Earths");
  263. } else if (kg < 1.4e39) {
  264. let mass = round(kg/2e30,2);
  265. return mass + (singular || mass == 1 ? " Sun" :" Suns");
  266. } else {
  267. let mass = round(kg/1.4e42,2);
  268. return mass + (singular || mass == 1 ? " Milky Way" :" Milky Ways");
  269. }
  270. }
  271. function metricLength(m, singular=false) {
  272. if (m < 1/100) {
  273. let length = round(m * 1000,2);
  274. return length + (singular || length == 1 ? " millimeter" : " millimeters");
  275. } else if (m < 1) {
  276. let length = round(m * 100,0);
  277. return length + (singular || length == 1 ? " centimeter" : " centimeters");
  278. } else if (m < 500) {
  279. let length = round(m,2);
  280. return length + (singular || length == 1 ? " meter" : " meters");
  281. } else {
  282. let length = round(m / 1000,1);
  283. return length + (singular || length == 1 ? " kilometer" : " kilometers");
  284. }
  285. }
  286. function metricSymLength(m, singular=false) {
  287. if (m < 1/100) {
  288. let length = round(m * 1000,2);
  289. return length + " mm";
  290. } else if (m < 1) {
  291. let length = round(m * 100,0);
  292. return length + " cm";
  293. } else if (m < 500) {
  294. let length = round(m,2);
  295. return length + " m";
  296. } else {
  297. let length = round(m / 1000,1);
  298. return length + " km";
  299. }
  300. }
  301. function customaryLength(m, singular=false) {
  302. let ft = m * 3.28084;
  303. if (ft < 1) {
  304. let length = round(ft * 12,0);
  305. return length + (singular || length == 1 ? " inch" : " inches");
  306. } else if (ft < 5280) {
  307. let end = customaryLength((ft - Math.floor(ft))/3.28084, singular);
  308. let length = Math.floor(ft);
  309. return length + (singular || length == 1 ? " foot" : " feet") + " " + end;
  310. } else {
  311. let length = round(ft/5280,1);
  312. return length + (singular || length == 1 ? " mile" : " miles");
  313. }
  314. }
  315. function customarySymLength(m, singular=false) {
  316. let ft = m * 3.28084;
  317. if (ft < 1) {
  318. let length = round(ft * 12,0);
  319. return length + "\"";
  320. } else if (ft < 5280) {
  321. let end = customarySymLength((ft - Math.floor(ft))/3.28084, singular);
  322. let length = Math.floor(ft);
  323. return length + "'" + " " + end;
  324. } else {
  325. let length = round(ft/5280,1);
  326. return length + " mi";
  327. }
  328. }
  329. function approxLength(m, singular=false) {
  330. if (m < 25) {
  331. let length = round(m/1.9,1);
  332. return length + (singular || length == 1 ? " person" : " people");
  333. } else if (m < 350) {
  334. let length = round(m/49,1);
  335. return length + (singular || length == 1 ? " football field" : " football fields");
  336. } else if (m < 20000) {
  337. let length = round(m/449,1);
  338. return length + (singular || length == 1 ? " Empire State Building" : " Empire State Buildings");
  339. } else if (m < 2000000) {
  340. let length = round(m/80467.2,1);
  341. return length + (singular || length == 1 ? " Panama Canal" : " Panama Canals");
  342. } else if (m < 3474574*2) {
  343. let length = round(m/3474574,1);
  344. return length + (singular || length == 1 ? " Moon" : " moons");
  345. } else if (m < 12.742e6*130) {
  346. let length = round(m/12.742e6,2);
  347. return length + (singular || length == 1 ? " Earth" : " earths");
  348. } else if (m < 149.6e12) {
  349. let length = round(m/149.6e9,3);
  350. return length + (singular || length == 1 ? " AU" : " AUs");
  351. } else if (m < 9.4607e22) {
  352. let length = round(m/9.4607e15,3);
  353. return length + (singular || length == 1 ? " light year" : " light years");
  354. } else if (m < 5e26) {
  355. let length = round(m/9.4607e21,3);
  356. return length + (singular || length == 1 ? " galaxy" : " galaxies");
  357. } else {
  358. let length = round(m/4.40e26,3);
  359. return length + (singular || length == 1 ? " universe" : " universes");
  360. }
  361. }
  362. function metricArea(m2, singular=false) {
  363. if (m2 < 1/10) {
  364. let area = round(m2 * 10000,2);
  365. return area + (singular || area == 1 ? " square centimeter" : " square centimeters");
  366. } else if (m2 < 100000) {
  367. let area = round(m2,2);
  368. return area + (singular || area == 1 ? " square meter" : " square meters");
  369. } else {
  370. let area = round(m2 / 1e6,2);
  371. return area + (singular || area == 1 ? " kilometer" : " square kilometers");
  372. }
  373. }
  374. function metricSymArea(m2, singular=false) {
  375. if (m2 < 1/10) {
  376. let area = round(m2 * 10000,2);
  377. return area + " cm" + "2".sup();
  378. } else if (m2 < 100000) {
  379. let area = round(m2,2);
  380. return area + " m" + "2".sup();
  381. } else {
  382. let area = round(m2 / 1e6,2);
  383. return area + " km" + "2".sup();
  384. }
  385. }
  386. function customaryArea(m2, singular=false) {
  387. let ft2 = m2 * 3.28084 * 3.28084;
  388. if (ft2 < 1) {
  389. let area = round(ft2 * 144,0);
  390. return area + (singular || area == 1 ? " square inch" : " square inches");
  391. } else if (ft2 < 5280 * 5280 / 10) {
  392. let area = round(ft2,1);
  393. return area + (singular || area == 1 ? " square foot" : " square feet");
  394. } else {
  395. let area = round(ft2 / 5280 / 5280,1);
  396. return area + (singular || area == 1 ? " square mile" : " square miles");
  397. }
  398. }
  399. function customarySymArea(m2, singular=false) {
  400. if (m2 < 1/10) {
  401. let area = round(m2 * 10000,2);
  402. return area + " in" + "2".sup();
  403. } else if (m2 < 100000) {
  404. let area = round(m2,2);
  405. return area + " ft" + "2".sup();
  406. } else {
  407. let area = round(m2 / 1e6,2);
  408. return area + " mi" + "2".sup();
  409. }
  410. }
  411. function metricVolume(m3, singular=false) {
  412. if (m3 < 1/1000) {
  413. let volume = round(m3*1e6, 0);
  414. return volume + (singular || volume == 1 ? " milliliter" : " milliliters");
  415. } else if (m3 < 1) {
  416. let volume = round(m3*1000, 1);
  417. return volume + (singular || volume == 1 ? " liter" : " liters");
  418. } else if (m3 < 1000000) {
  419. let volume = round(m3, 0);
  420. return volume + (singular || volume == 1 ? " cubic meter" : " cubic meters");
  421. } else if (m3 < 1e12){
  422. let volume = round(m3/1e9, 3);
  423. return volume + (singular || volume == 1 ? " cubic kilometer" : " cubic kilometers");
  424. } else {
  425. let volume = round(m3/1e9, 0);
  426. return volume + (singular || volume == 1 ? " cubic kilometer" : " cubic kilometers");
  427. }
  428. }
  429. function metricSymVolume(m3, singular=false) {
  430. if (m3 < 1/1000) {
  431. let volume = round(m3*1e6, 0);
  432. return volume + " mL";
  433. } else if (m3 < 1) {
  434. let volume = round(m3*1000, 1);
  435. return volume + " L";
  436. } else if (m3 < 1000000) {
  437. let volume = round(m3, 0);
  438. return volume + " m" + "3".sup();
  439. } else if (m3 < 1e12){
  440. let volume = round(m3/1e9, 3);
  441. return volume + " km" + "3".sup();
  442. } else {
  443. let volume = round(m3/1e9, 0);
  444. return volume + " km" + "3".sup();
  445. }
  446. }
  447. function customaryVolume(m3, singular=false) {
  448. let gallons = m3 * 264.172;
  449. if (gallons < 1/16) {
  450. let volume = round(gallons*128,0);
  451. return volume + (singular || volume == 1 ? " fluid ounce" : " fluid ounces");
  452. } else if (gallons < 1/4) {
  453. let volume = round(gallons*16,1);
  454. return volume + (singular || volume == 1 ? " cup" : " cups");
  455. } else if (gallons < 1/2) {
  456. let volume = round(gallons*8,1);
  457. return volume + (singular || volume == 1 ? " pint" : " pints");
  458. } else if (gallons < 1) {
  459. let volume = round(gallons*4,1);
  460. return volume + (singular || volume == 1 ? " quart" : " quarts");
  461. } else if (gallons < 100) {
  462. let volume = round(gallons,1);
  463. return volume + (singular || volume == 1 ? " gallon" : " gallons");
  464. } else {
  465. let volume = round(gallons,0);
  466. return volume + (singular || volume == 1 ? " gallon" : " gallons");
  467. }
  468. }
  469. function customarySymVolume(m3, singular=false) {
  470. let gallons = m3 * 264.172;
  471. if (gallons < 1/16) {
  472. let volume = round(gallons*128,0);
  473. return volume + " fl oz";
  474. } else if (gallons < 1/4) {
  475. let volume = round(gallons*16,1);
  476. return volume + (singular || volume == 1 ? " cp" : " cps");
  477. } else if (gallons < 1/2) {
  478. let volume = round(gallons*8,1);
  479. return volume + " pt";
  480. } else if (gallons < 1) {
  481. let volume = round(gallons*4,1);
  482. return volume + " qt";
  483. } else if (gallons < 100) {
  484. let volume = round(gallons,1);
  485. return volume + " g";
  486. } else {
  487. let volume = round(gallons,0);
  488. return volume + " g";
  489. }
  490. }
  491. function approxVolume(m3, singular=false) {
  492. if (m3 < 2/10000) {
  493. let volume = round(m3*4e5,0);
  494. return volume + (singular || volume == 1 ? " shot" : " shots");
  495. } else if (m3 < .1) {
  496. let volume = round(m3*2254,1);
  497. return volume + (singular || volume == 1 ? " glass" : " glasses");
  498. } else if (m3 < 100) {
  499. let volume = round(m3*2.64,1);
  500. return volume + (singular || volume == 1 ? " bathtub" : " bathtubs");
  501. } else if (m3 < 1e5) {
  502. let volume = round(m3/1000,2);
  503. return volume + (singular || volume == 1 ? " Olympic swimming pool" : " Olympic swimming pools");
  504. } else if (m3 < 1e9) {
  505. let volume = round(m3/3.2e5,2);
  506. return volume + (singular || volume == 1 ? " oil tanker" : " oil tankers");
  507. } else if (m3 < 1e15) {
  508. let volume = round(m3/1.8919e10,3);
  509. return volume + (singular || volume == 1 ? " Great Salt Lake" : " Great Salt Lakes");
  510. } else if (m3 < 1e20){
  511. let volume = round(m3/3.547e17, 3);
  512. return volume + (singular || volume == 1 ? " ocean" : " oceans");
  513. } else if (m3 < 1e25){
  514. let volume = round(m3/1e21, 3);
  515. return volume + (singular || volume == 1 ? " Earth" : " Earths");
  516. } else {
  517. let volume = round(m3/1.4e27, 3);
  518. return volume + (singular || volume == 1 ? " Sun" : " Suns");
  519. }
  520. }
  521. function makeSphere(input=0, diameter=false) {
  522. if (diameter = true) {
  523. input = input/2;
  524. }
  525. return (4/3)*Math.PI*(Math.pow(input, 3));
  526. }
  527. function breakSphere(input=0, diameter=false) {
  528. let output = math.pow((3*input)/(4*Math.PI), 1/3)
  529. if (diameter=true) {
  530. output = output*2;
  531. }
  532. return output;
  533. }