question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

toSI() on a scalar

See original GitHub issue

Is there a way to make toSI() work on a scalar? The use case is when I want to display the result of an expression in SI unit if it has units. Like this:

  • math.evaluate(‘1 cm’).toSI() --> 0.01 m
  • math.evaluete(‘2cm / 1cm’).toSI() --> 2

Or, is there a function that works like hasunit in the following example?

if (hasunit(var)) {
print(var.toSI());
}
else {
print(var);
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rnd-debugcommented, Sep 22, 2020

I also wasn’t confortable with extending JS Number, so I assume that I can close the PR 😇 One dirty code snippet:

const mathjs =  require('mathjs')
const config = {predictable :true }
const math = mathjs.create(mathjs.all, config)
console.log(math.typeOf(math.evaluate('4m / 1m')))         // Unit
console.log(math.evaluate('4m / 1m').toSI().toString())      // 4
0reactions
josdejongcommented, Sep 23, 2020

👍

Thanks @rnd-debug for all your inputs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NumberQuantity (Units of Measurement Reference Implementation ...
Returns the scalar quantity for the specified short stated in the specified unit. Parameters: value - the quantity value. unit - the measurement...
Read more >
Succinctly implementing newtypes for multiple scalar units
It must not be possible to do erroneous calculations because values are in different units. The amount of code duplication must be limited....
Read more >
massless scalar particles: Topics by Science.gov
In this paper, we investigate the influences of external massless scalar field to quantum Prisoners' Dilemma (QPD) game. We firstly derive the master ......
Read more >
Search for third-generation scalar leptoquarks decaying to a ...
Abstract A search for pair production of heavy scalar lep- toquarks (LQs), each decaying into a top quark and a τ lep- ton,...
Read more >
LegalizerHelper Class Reference - LLVM
Legalize a vector instruction by splitting into multiple components, each acting on the same scalar type as the original but with fewer elements....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found