Skip to content

Instantly share code, notes, and snippets.

@CodeDrome
Created July 9, 2021 16:44
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 CodeDrome/5540a95fcc4d8128c159a1b3f250708a to your computer and use it in GitHub Desktop.
Save CodeDrome/5540a95fcc4d8128c159a1b3f250708a to your computer and use it in GitHub Desktop.
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
try
{
output(`math.add(distance, temperature): ${math.add(distance, temperature)}`);
}
catch (e)
{
output(`math.add(distance, temperature): ${e}`);
}
// dividing distance by time will give a result as km / minute
output(`math.divide(distance, time): ${math.divide(distance, time)}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment