Pitfalls with using scientific notation in JavaScript
Asked Answered
F

4

10

This question is not seeking developer code formatting opinions. Personally, I prefer to use scientific notation in my JS code when I can because I believe it is more readable. For me, 6e8 is more readable than 600000000. That being said, I am solely looking for potential risks and disadvantages specifying numbers in scientific notation in JS. I don't see it often in the wild and was wondering if there is technical reasoning for that or if it simply because of developer's druthers.

Foreignborn answered 22/9, 2017 at 14:10 Comment(5)
Not sure I understand the question. Are you wondering whether every environment supports this notation? Or whether you will get the value you are specifying?Obstetrics
@FelixKling, good point. I was thinking more along the lines of preserving the original value being specified. I'm open to any issues that might come up when using scientific notation in my JS, though.Foreignborn
@EvanWieland, not sure about of risks, but JS does allows sceintific notation. Wikipedia In most popular programming languages, 6.022E23 (or 6.022e23) is equivalent to 6.022×1023, and 1.6×10−35 would be written 1.6E-35 (e.g. Ada, Analytica, C/C++, FORTRAN (since FORTRAN II as of 1958), MATLAB, Scilab, Perl, Java,[4] Python, Lua, JavaScript, and others).Claudine
This link probably helps better to understand, why scientific notation cannot be processed as is without conversion. I do not think there is any risk or disadvantages. As long as the programming language supports it is all good. blog.penjee.com/binary-numbers-floating-point-conversionClaudine
I have to remember using this notation for big numbers to avoid counting zeroes with the mouse pointer...Tapis
O
3

You don't see scientific notation "often in the wild" because the only numbers that actually get typed in JS tend to be constants:

  • Code-centric constants (such as enums and levels) tend to be small.
  • Physical/mathematical constants (such as π or e) tend to be highly specific.

Neither of these benefit from scientific notation too much.

I have seen Plank's constant 'in the wild' as:

const h = 6.62607004e-34;
console.log('Plank', h);

The other place it often makes sense is time limits, for instance the number of ms in a day as 864e5. For instance:

function addDaysToDate(date, days) {
  if (days === 0)
    return date;
  date.setTime(864e5 * days + date.valueOf());
  return date;
}

const now = new Date();
const thisTimeTomorrow = addDaysToDate(now, 1);
console.log('This time tomorrow', thisTimeTomorrow);

I don't think there's any technical reason not to use this notation, it's more that developers avoid hard coding numbers at all.

I don't think there are any risks. You may have to be careful with numbers in strings, but if you're doing that then this syntax is a far smaller issue than, say, number localisation (for instance a DE user entering "20.000,00", expecting 2e4, but getting 2e6 thanks to invariant number formatting swapping the thousand and decimal separators).

I'd add that JS will output that syntax by default anyway for small numbers, but avoids for large numbers up to a point (which varies by browser):

console.log('Very small', 1234 / 100000000000)
console.log('Large, but still full in some browsers', 1e17 * 1234)
console.log('Large, scientific', 1e35 * 1234)
Ouch answered 28/9, 2017 at 14:48 Comment(5)
Timeouts such as 60e3ms often are constants in a range that would seem reasonable for scientific notation.Bouse
@Bouse ah, very good point, and I have seen that too! I'll update the answer.Ouch
I don't understand the sample of the DE user (I suppose it means German user). It wouldn't be 2e4 (20000) with German decimal comma and 2e1 (20) with English decimal point?Tapis
@AlbertoMartinez It depends on the localised parser - either way it doesn't come out right. Yeah it means user with German locality; a system I once worked on found a bug that effectively multiplied thousands of German input salaries by 100 (thanks to e4 becoming e6) - that was fun to fix :-| My point is: if you're passing round numbers as strings i18n is a much bigger issue than scientific notation - the latter is far more consistently parsed - I'd rather have "2e4" than "20,000.00" or "20.000,00" any day.Ouch
I agree, I was asking only for clarification on e-notation, living in a country that uses the same decimal marks than Germany means that I'm pretty aware of that issues :-)Tapis
S
1

From O. R. Mapper in this question:

Human users are not the only ones who want to read numbers. It seems D3 will throw an exception when encountering a translate transformation that contains coordinates in scientific notation

In addition, if you want change the string representation, as opposed to just what the literal looks like in your source, you'll have to be careful with serialized/stored data.

Also, from experience, often times you can have large numbers whose significance is in their individual digits like an ID or phone number. In this case, reducing these numbers to scientific notation hurts readability.

Sassanid answered 22/9, 2017 at 14:26 Comment(3)
I don’t see how this quote is relevant here. The question you linked to is about how JavaScript converts large numbers to strings. It’s not about specifying numbers in scientific notation (this question).Obstetrics
You're right, its only relevant if he does what I mentioned in my second sentence: In addition, if you want change the string representation, as opposed to just what the literal looks like in your source... Should have put that first.Sassanid
But that happens regardless of using scientific notation. It happens to all numbers larger than a certain value.Obstetrics
U
0

E-notation indicates a number that should be multiplied by 10 raised to a given power.

is not scientific exponential notation . One pitfall is that e "times ten raised to the power of" in JavaScript is not The number e the base of the natural logarithm, represented at browser as Math.E. For individuals familiar with the mathematical constant e, JavaScript e has an entirely different meaning. 6 * Math.pow(10, 8) returns expected result and does not include use of the JavaScript artifact e.

Although the E stands for exponent, the notation is usually referred to as (scientific) E-notation rather than (scientific) exponential notation. The use of E-notation facilitates data entry and readability in textual communication since it minimizes keystrokes, avoids reduced font sizes and provides a simpler and more concise display, but it is not encouraged in publications. Submission Guidelines for Authors: HPS 2010 Midyear Proceedings

Unwieldy answered 1/10, 2017 at 22:28 Comment(0)
H
0

Another method to represent numbers for readability is using underscore separators:

const myNumber: number = 1_000_999.123_456; // 1000999.123456

https://caniuse.com/mdn-javascript_grammar_numeric_separators

Additional methods: https://v8.dev/features/numeric-separators

Himelman answered 20/8 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.