简体   繁体   English

在JavaScript中使用科学记数法的陷阱

[英]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. 就个人而言,我更喜欢在我的JS代码中使用科学记数法,因为我相信它更具可读性。 For me, 6e8 is more readable than 600000000 . 对我来说, 6e8600000000更具可读性。 That being said, I am solely looking for potential risks and disadvantages specifying numbers in scientific notation in JS. 话虽这么说,我只是在寻找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. 我不经常在野外看到它,并且想知道是否有技术推理,或仅仅是因为开发人员的笨蛋。

You don't see scientific notation "often in the wild" because the only numbers that actually get typed in JS tend to be constants: 你没有看到“经常在野外”的科学记数法因为实际上在JS中输入的唯一数字往往是常数:

  • Code-centric constants (such as enums and levels) tend to be small. 以代码为中心的常量(例如枚举和级别)往往很小。
  • Physical/mathematical constants (such as π or e) tend to be highly specific. 物理/数学常数(例如π或e)往往具有高度特异性。

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 . 通常有意义的另一个地方是时间限制,例如一天中的ms数为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). 您可能必须小心字符串中的数字,但如果您这样做,那么这种语法比数字本地化(例如DE用户输入"20.000,00" ,期望2e4 ,但获得)要2e42e6感谢不变的数字格式交换千位和小数分隔符)。

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): 我想补充一点,JS默认会输出那些语法,但是对于一些小数字,它可以避免大量数据(根据浏览器的不同而不同):

 console.log('Very small', 1234 / 100000000000) console.log('Large, but still full in some browsers', 1e17 * 1234) console.log('Large, scientific', 1e35 * 1234) 

From OR Mapper in this question: 来自OR Mapper的这个问题:

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 当遇到包含科学记数法坐标的转换变换时,D3似乎会抛出异常

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. 此外,根据经验,您通常可以拥有大数字,其重要性在于其个人数字,如ID或电话号码。 In this case, reducing these numbers to scientific notation hurts readability. 在这种情况下,将这些数字减少到科学记数法会损害可读性。

E-notation indicates a number that should be multiplied by 10 raised to a given power. 电子记表示一个数字应该乘以给定功率的10倍。

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 . 其中一个缺陷是e “十倍提高到的力量”在JavaScript中没有The number e的自然对数的底数,在浏览器为代表Math.E For individuals familiar with the mathematical constant e , JavaScript e has an entirely different meaning. 对于熟悉数学常数e ,JavaScript e具有完全不同的含义。 6 * Math.pow(10, 8) returns expected result and does not include use of the JavaScript artifact e . 6 * Math.pow(10, 8)返回预期结果,不包括使用JavaScript工件e

Although the E stands for exponent, the notation is usually referred to as (scientific) E-notation rather than (scientific) exponential notation. 虽然E代表指数,但符号通常被称为(科学)E符号而不是(科学)指数符号。 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 作者提交指南:HPS 2010年中期会议录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM