Skip to content

Instantly share code, notes, and snippets.

@CodeDrome
CodeDrome / mathjsbignumber.js
Created Aug 29, 2021
mathjsbignumber.js - part 3
View mathjsbignumber.js
function BigNumbers()
{
output("MathJS Big Numbers<br>==================<br><br>");
output(`Size of observable universe in kilometres:
${math.format(math.bignumber(880000000000000000000000000),
{notation: 'fixed'})}
(880,000,000,000,000,000,000,000,000)`);
output(`<br>math.add(math.bignumber(10), math.bignumber(13)) = ${math.add(math.bignumber(10), math.bignumber(13))}`);
@CodeDrome
CodeDrome / mathjsbignumber.js
Created Aug 29, 2021
mathjsbignumber.js - part 2
View mathjsbignumber.js
function BigInts()
{
output("BigInt<br>------<br><br>");
const big1 = 9007199254740992n; // 9,007,199,254,740,992
const big2 = 9007199254740993n; // 9,007,199,254,740,993
output(`typeof(big1) = ${typeof(big1)}`);
output(`<br>big1 = ${big1}`);
@CodeDrome
CodeDrome / mathjsbignumber1.js
Created Aug 29, 2021
mathjsbignumber.js - part 1
View mathjsbignumber1.js
"use strict"
window.onload = function()
{
safeIntegers();
// BigInts();
// BigNumbers();
View mathjsunits10.js
function bitsAndBytes()
{
const bits = math.unit(64, 'bits');
const bytes = bits.to('bytes');
const kilobytes = math.unit(1024, 'bytes');
output(`bits: ${bits}`);
output(`bytes: ${bytes}`);
output(`kilobytes: ${kilobytes}`);
output(`kilobytes to kibibytes: ${kilobytes.to('kibibytes')}`);
View mathjsunits09.js
function gallons()
{
// create imperial gallon unit
math.createUnit('impgal', '4.54609 liter');
// could do this, but probably not advisable!
// math.createUnit('gallon', '4.54609 liter', {override: true})
const usgallon = math.unit(1, 'gallon');
output(`usgallon: ${usgallon}`);
View mathjsunits08.js
function power()
{
// demonstrate creating custom units
// this is metric horsepower, most commonly denoted by PS
// for pferd starke, German for horsepower
math.createUnit('ps', '735.49875 watt');
// The math.js hp is mechanical horsepower or approx 746 Watts
const power = math.unit(100, 'hp');
View mathjsunits07.js
function gravity()
{
// calculate the weight of a person on massMars
// the values needed as math.unit objects
const massPerson = math.unit(75, 'kg');
const massMars = math.unit(6.4171e23, 'kg');
const radiusMars = math.unit(3389500, 'm');
// implement formula in 4 stages using math.js methods
View mathjsunits06.js
function astronomical()
{
// create length units for astronomy
math.createUnit('lightyear', '9460730777119.56 km');
math.createUnit('parsec', '30856775714409.19 km');
math.createUnit('AU', '149597870.7 km');
// Proxima Centauri is the nearest star to the Sun
const EarthToProximaCentauri = math.unit(4.2465, 'lightyear');
output(`EarthToProximaCentauri: ${EarthToProximaCentauri}`);
View mathjsunits05.js
function compatibility()
{
// different types
const distance = math.unit(120, 'km');
const temperature = math.unit(20, 'degC');
const time = math.unit(60, 'minute');
// here we are trying to add km and degC which
// is meaningless and will throw an exception
View mathjsunits04.js
function valueProperty()
{
const centimetres = math.unit(50, 'cm');
output(`centimetres: ${centimetres}`);
output(`centimetres.value: ${centimetres.value}`);
// setting value will give a result in metres
// despite the unit being cm