Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NaN and Infinity not allowed #148

Open
vallsv opened this issue Apr 30, 2024 · 3 comments
Open

NaN and Infinity not allowed #148

vallsv opened this issue Apr 30, 2024 · 3 comments

Comments

@vallsv
Copy link

vallsv commented Apr 30, 2024

Hi,

While doing some tests, i found out that it is not possible to create quantities with NaN or Infinity.

I would like to know if it's an expected behavior.

import Qty from "js-quantities";

try {
  new Qty(Infinity, 'mm');
} catch (e) {
  console.log("Oupsi", e.message);
}

try {
  new Qty(NaN, 'mm');
} catch (e) {
  console.log("Oupsi...", e.message);
}

// Oupsi... Only number accepted as initialization value when units are explicitly provided

I am working on a system with python server. So i try to see if i can transpose everything on both sides.
For example, here is with Pint.

>>> import pint
>>> pint.Quantity(float('nan'), 'mm') / pint.Quantity(float('nan'), 's')
<Quantity(nan, 'millimeter / second')>

I was guessing javascript was already propagating NaN and Infinity properly inside js-quantities.
What do you think?

@vallsv
Copy link
Author

vallsv commented May 1, 2024

BTW, if you like the idea, i can maybe try to do a PR. Tell me.

@glepretre
Copy link
Contributor

glepretre commented May 3, 2024

I'm not sure to understand your use case? You would prefer to see NaN or Infinity rather than an error? 🤔

Even if it's not by design, I think it's better for js-quantities to refuse NaN or Inifinity as quantities scalar. These cases are more likely to be a bug rather than a feature for users.

@vallsv
Copy link
Author

vallsv commented May 3, 2024

Exactly, i would like to have NaN and infinity, because that's float values like any others. Javascript already handle this cases with rules which are part of standards, that's the principle of least astonishment.

I am not really sure people are aware that operators could throw such exceptions, because it doesn't behave like javascript. But i understand your point. I am working on scientific software stack, i have to deal with such possibilities. But it's probably not a common use case.

BTW I did play a bit to see how it behave in some side corners. I was able to trick with unitless quantities. Maybe there is some cases you would like to fix anyway (tested with version 1.7.6).

  const big = new Qty(1e200);
  const inf = big.mul(big);
  console.log(`${inf}`);                  // this logs "Infinity"

  // console.log(`${zero.div(zero)}`);    // throw division by zero

  const nan = inf.sub(inf);
  console.log(`${nan}`);                  // this logs "NaN"

  const mm = new Qty(1e200, 'mm');
  const s = new Qty(1e200, 's');
  // console.log(`${mm.mul(s)}`);         // throw other.isTemperature is not a function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants