Created
July 9, 2021 17:12
-
-
Save CodeDrome/962365b47678a84c2baf85fbd15c2013 to your computer and use it in GitHub Desktop.
mathjsunits07.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
const numerator = math.multiply(massPerson, massMars); | |
const denominator = math.pow(radiusMars, 2); | |
const quotient = math.divide(numerator, denominator); | |
const newtons = math.multiply(math.gravitationConstant, quotient); | |
// convert Newtons to kg | |
const weightOnMars = math.unit(math.divide(newtons.toNumber(), 9.80665), 'kg'); | |
output(`massPerson: ${massPerson}`); | |
output(`massMars: ${massMars.to('kg')}`); | |
output(`radiusMars: ${radiusMars.to('km')}`); | |
output(`Newtons: ${newtons}`); | |
output(`weightOnMars: ${weightOnMars.to('kg')}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment