Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pitfalls with using scientific notation in JavaScript

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.

like image 472
Evan Wieland Avatar asked Sep 22 '17 14:09

Evan Wieland


People also ask

Can you use scientific notation in JavaScript?

JavaScript converts any floating-point value with at least six trailing zeros into an annotation by default. 10e1 is 100 and 10e-1 is 1 . You can easily expand this to see that 10eN * 10e-N is always 100 . If you want true scientific notation, as in 1 * 10^2 , you want 1e12 and 1e-12 .

What is exponential notation in JavaScript?

The toExponential() method in JavaScript is used to convert a number to its exponential form. It returns a string representing the Number object in exponential notation. The toExponential() method is used with a number as shown in the above syntax using the '.

How do I print large numbers in JavaScript?

To store large numbers in JavaScript, use BigInt() rather than + operator. If you will use the + operator, then expect loss of precision.


3 Answers

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)
like image 63
Keith Avatar answered Nov 04 '22 04:11

Keith


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

like image 30
guest271314 Avatar answered Nov 04 '22 04:11

guest271314


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.

like image 40
code11 Avatar answered Nov 04 '22 06:11

code11