C++ 1e-9 或 -1e9,哪个是正确的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12134345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:55:05  来源:igfitidea点击:

1e-9 or -1e9, which one is correct?

c++mathfloating-pointnotationexponential

提问by Joan Venge

I am assigned some old code and when I was reading through it, I noticed it had these in the form of:

我被分配了一些旧代码,当我通读它时,我注意到它的形式如下:

float low = 1e-9;
float high = 1e9;

float lowB = 1e-9;
float highB = 1e9;

float lowL = 1e-9;
float highL = 1e9;

So I see that it's trying to define some ranges using the e notation, right? But isn't 1e-9supposed to be -1e9?

所以我看到它正在尝试使用 e 表示法定义一些范围,对吗?但不1e-9应该是-1e9吗?

Then the values would be between -1000000000and 1000000000, right?

那么值将介于-1000000000和之间1000000000,对吗?

I am not sure what 1e-9is meant for?

我不确定是什么1e-9意思?

回答by Keith Thompson

Neither is more correct than the other. They just represent different values.

两者都不比另一个更正确。它们只是代表不同的值。

1e-9is 0.000000001; the minus sign applies to the exponent.

1e-90.000000001;减号适用于指数。

-1e9is -1000000000.0; the minus sign applies to the number itself.

-1e9-1000000000.0;减号适用于数字本身。

The e(or E) means "times 10-to-the", so 1e9is "one times ten to the ninth power", and 1e-9means "one times ten to the negative ninth power". In mathematical scientific notation, this is usually denoted by a superscript: 1 × 10-9or -1 × 109. Programming languages adopted the eor Enotation because it was easier to type and print than a superscript (and still is, for that matter). (I think this may have been introduced by Fortran in the 1950s, but I'm not sure of the exact history.)

e(或E)装置“乘以10至的”,所以1e9是“一乘以十第九功率”,并1e-9指“一个乘以十到负第九功率”。在数学科学记数法中,这通常用上标表示:1 × 10 -9或 -1 × 10 9。编程语言采用eorE符号是因为它比上标更容易输入和打印(并且仍然如此)。(我认为这可能是 Fortran 在 1950 年代引入的,但我不确定确切的历史。)

回答by Kerrek SB

"Low" and "high" refer to absolute values. The low number is small in absolute values, the high one is large. Negatives aren't important, since you already understand how to work with those. What's important about floats is their variable scale(i.e. the exponent), and so it is customary to provide lower and upper bounds for the scale rather than the value.

“低”和“高”是指绝对值。低数绝对值小,高数大。负数并不重要,因为您已经了解如何使用它们。浮点数的重要之处在于它们的可变比例(即指数),因此习惯上提供比例的下限和上限而不是值。