How to use scientific notation in js?

风流意气都作罢 提交于 2020-01-24 14:14:48

问题


var a = 5.0;
var b = a * 10e-12;
b *= 10e+12
print(b)

Why b equals 500 instead of 5?

As far as I know 10^(-12) equals to 1/(10^12), how can i rewrite the code?


回答1:


Because math. 10e1 is 100 and 10e-1 is 1.

  • 10e1 * 10e-1 is 100.
  • 10e2 * 10e-2 is 100.
  • 10e3 * 10e-3 is 100.

You can very easily extend this to figure out that 10eN * 10e-N is always going to be 100.

If you want actual scientific notation, as in 1 * 10 ^ 2, you want 1e12 and 1e-12.




回答2:


10-12 × 1012 = 1

But what you wrote wasn't 10-12, nor did you write 1012.

What you wrote was 10 × 1012 and 10 × 10-12:

10 × 1012 × 10 × 10-12 = 100

100 × 5 = 500

Proper scientific notation is 1e-12 and 1e12. The e stands for "ten to the power of", so you don't need to multiply that value by ten again.




回答3:


"As far as I know 10^(-12) equals to 1/(10^12)" -- that is correct, but 10e-12 actually means 10*10^(-12)



来源:https://stackoverflow.com/questions/27151311/how-to-use-scientific-notation-in-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!