I've read about the difference between double precision and single precision. However, in most cases, float
and double
seem to be interchangeable, i.e. using one or the other does not seem to affect the results. Is this really the case? When are floats and doubles interchangeable? What are the differences between them?
我读过关于双精度和单精度的区别。然而,在大多数情况下,float和double似乎是可以互换的,即使用其中一个或另一个似乎不会影响结果。真的是这样吗?什么时候浮点数和双精度数可以互换?他们之间有什么不同?
11 个解决方案
#1
385
Huge difference.
巨大的差异。
As the name implies, a double
has 2x the precision of float
[1]. In general a double
has 15 decimal digits of precision, while float
has 7.
顾名思义,double的精度是float[1]的两倍。一般来说,双精度为15位,浮点数为7。
Here's how the number of digits are calculated:
下面是计算数字的方法:
double
has 52 mantissa bits + 1 hidden bit: log(253)÷log(10) = 15.95 digits双有52位+ 1隐位尾数:日志(253)÷日志(10)= 15.95位
float
has 23 mantissa bits + 1 hidden bit: log(224)÷log(10) = 7.22 digits浮动有23位+ 1隐位尾数:日志(224)÷日志(10)= 7.22位
This precision loss could lead to truncation errors much easier to float up, e.g.
这种精度损失可能导致截断误差更容易浮动,例如。
float a = 1.f / 81;
float b = 0;
for (int i = 0; i < 729; ++ i)
b += a;
printf("%.7g\n", b); // prints 9.000023
while
而
double a = 1.0 / 81;
double b = 0;
for (int i = 0; i < 729; ++ i)
b += a;
printf("%.15g\n", b); // prints 8.99999999999996
Also, the maximum value of float is about 3e38
, but double is about 1.7e308
, so using float
can hit "infinity" (i.e. a special floating-point number) much more easily than double
for something simple, e.g. computing the factorial of 60.
另外,float的最大值大约是3e38,而double大约是1.7e308,所以使用float比double更容易达到“infinity”(即一个特殊的浮点数),例如计算60的阶乘。
During testing, maybe a few test cases contain these huge numbers, which may cause your programs to fail if you use floats.
在测试期间,可能有几个测试用例包含这些巨大的数字,如果使用浮点数,可能会导致程序失败。
Of course, sometimes, even double
isn't accurate enough, hence we sometimes have long double
[1] (the above example gives 9.000000000000000066 on Mac), but all floating point types suffer from round-off errors, so if precision is very important (e.g. money processing) you should use int
or a fraction class.
当然,有时甚至连double都不够精确,因此有时我们有long double[1](上面的示例在Mac上给出了9.000000000000000000000000000066),但是所有浮点类型都有舍入错误,所以如果精度非常重要(例如货币处理),您应该使用int类或分数类。
Furthermore, don't use +=
to sum lots of floating point numbers, as the errors accumulate quickly. If you're using Python, use fsum
. Otherwise, try to implement the Kahan summation algorithm.
此外,不要使用+=和大量浮点数,因为错误会迅速累积。如果您正在使用Python,请使用fsum。否则,尝试实现Kahan求和算法。
[1]: The C and C++ standards do not specify the representation of float
, double
and long double
. It is possible that all three are implemented as IEEE double-precision. Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float
is indeed a IEEE single-precision floating point number (binary32), and double
is a IEEE double-precision floating point number (binary64).
[1]: C和c++标准没有指定浮点、双、长双的表示。这三种方法都可以实现为IEEE双精度。然而,对于大多数架构(gcc, MSVC;x86、x64、ARM) float实际上是IEEE单精度浮点数(binary32), double是IEEE双精度浮点数(binary64)。
#2
49
Here is what the standard C99 (ISO-IEC 9899 6.2.5 §10) or C++2003 (ISO-IEC 14882-2003 3.1.9 §8) standards say:
这就是C99标准(9899年ISO-IEC 6.2.5§10)或C + + 2003(ISO-IEC 14882 - 2003 3.1.9§8)标准说:
There are three floating point types:
float
,double
, andlong double
. The typedouble
provides at least as much precision asfloat
, and the typelong double
provides at least as much precision asdouble
. The set of values of the typefloat
is a subset of the set of values of the typedouble
; the set of values of the typedouble
is a subset of the set of values of the typelong double
.有三种浮点类型:浮点类型、双类型和长双类型。类型double提供的精度至少与浮点数相同,类型long double提供的精度至少与double相同。类型float的值集是类型double的值集的子集;类型double的值集是类型long double的值集的子集。
The C++ standard adds:
c++标准补充道:
The value representation of floating-point types is implementation-defined.
浮点类型的值表示是实现定义的。
I would suggest having a look at the excellent What Every Computer Scientist Should Know About Floating-Point Arithmetic that covers the IEEE floating-point standard in depth. You'll learn about the representation details and you'll realize there is a tradeoff between magnitude and precision. The precision of the floating point representation increases as the magnitude decreases, hence floating point numbers between -1 and 1 are those with the most precision.
我建议大家看看每一个计算机科学家都应该了解的关于浮点算法的优秀知识,这些算法深入地涵盖了IEEE浮点标准。您将了解表示细节,并认识到大小和精度之间存在权衡。浮点表示法的精度随着量值的减小而增大,因此在-1和1之间的浮点数是最精确的。
#3
24
Given a quadratic equation: x2 − 4.0000000 x + 3.9999999 = 0, the exact roots to 10 significant digits are, r1 = 2.000316228 and r2 = 1.999683772.
给定一个二次方程:x2−4.0000000 x + 3.9999999 = 0,精确到10根有效数字,r1 = 2.000316228和r2 = 1.999683772。
Using float
and double
, we can write a test program:
使用float和double,我们可以编写一个测试程序:
#include <stdio.h>
#include <math.h>
void dbl_solve(double a, double b, double c)
{
double d = b*b - 4.0*a*c;
double sd = sqrt(d);
double r1 = (-b + sd) / (2.0*a);
double r2 = (-b - sd) / (2.0*a);
printf("%.5f\t%.5f\n", r1, r2);
}
void flt_solve(float a, float b, float c)
{
float d = b*b - 4.0f*a*c;
float sd = sqrtf(d);
float r1 = (-b + sd) / (2.0f*a);
float r2 = (-b - sd) / (2.0f*a);
printf("%.5f\t%.5f\n", r1, r2);
}
int main(void)
{
float fa = 1.0f;
float fb = -4.0000000f;
float fc = 3.9999999f;
double da = 1.0;
double db = -4.0000000;
double dc = 3.9999999;
flt_solve(fa, fb, fc);
dbl_solve(da, db, dc);
return 0;
}
Running the program gives me:
运行这个程序给了我:
2.00000 2.00000
2.00032 1.99968
Note that the numbers aren't large, but still you get cancellation effects using float
.
注意,这些数字并不大,但是仍然可以使用float来获得取消效果。
(In fact, the above is not the best way of solving quadratic equations using either single- or double-precision floating-point numbers, but the answer remains unchanged even if one uses a more stable method.)
(事实上,上述方法并不是使用单精度浮点数或双精度浮点数求解二次方程的最佳方法,但即使使用更稳定的方法,其结果也不会改变。)
#4
17
- A double is 64 and single precision (float) is 32 bits.
- 双精度为64,单精度(浮点数)为32位。
- The double has a bigger mantissa (the integer bits of the real number).
- 双精度浮点数有一个较大的尾数(实数的整数位)。
- Any inaccuracies will be smaller in the double.
- 任何不准确的地方都将缩小。
#5
11
The size of the numbers involved in the float-point calculations is not the most relevant thing. It's the calculation that is being performed that is relevant.
浮点计算中涉及的数字的大小并不是最相关的。正在进行的计算是相关的。
In essence, if you're performing a calculation and the result is an irrational number or recurring decimal, then there will be rounding errors when that number is squashed into the finite size data structure you're using. Since double is twice the size of float then the rounding error will be a lot smaller.
本质上,如果您正在执行一个计算,结果是一个无理数或循环小数,那么当这个数字被压缩到您正在使用的有限大小的数据结构中时,将会出现四舍五入错误。由于double的大小是float大小的两倍,因此舍入误差会小得多。
The tests may specifically use numbers which would cause this kind of error and therefore tested that you'd used the appropriate type in your code.
测试可能会特别使用会导致这种错误的数字,因此测试您在代码中使用了适当的类型。
#6
8
Floats have less precision than doubles. Although you already know, read What WE Should Know About Floating-Point Arithmetic for better understanding.
浮点数的精度低于双精度数。尽管你已经知道了,为了更好的理解,阅读我们应该知道的浮点算术。
#7
8
Type float, 32 bits long, has a precision of 7 digits. While it may store values with very large or very small range (+/- 3.4 * 10^38 or * 10^-38), it has only 7 significant digits.
浮点类型,32位长,精度为7位。虽然存储值和非常大或非常小的范围(+ / - 3.4 * 10 ^ 38 * 10 ^ -38),它只有7位有效数字。
Type double, 64 bits long, has a bigger range (*10^+/-308) and 15 digits precision.
型双,64位长,有一个更大的范围(* 10 ^ + / -308)和15位精度。
Type long double is nominally 80 bits, though a given compiler/OS pairing may store it as 12-16 bytes for alignment purposes. The long double has an exponent that just ridiculously huge and should have 19 digits precision. Microsoft, in their infinite wisdom, limits long double to 8 bytes, the same as plain double.
长双字节在名义上是80位,但是给定的编译器/操作系统对可以将其存储为12-16字节以进行对齐。长倍的指数非常大,应该有19位数字的精度。微软拥有无限的智慧,将长双字节限制为8字节,就像普通双字节一样。
Generally speaking, just use type double when you need a floating point value/variable. Literal floating point values used in expressions will be treated as doubles by default, and most of the math functions that return floating point values return doubles. You'll save yourself many headaches and typecastings if you just use double.
一般来说,当需要浮点值/变量时,只需使用类型double。在表达式中使用的文字浮点值在默认情况下将被处理为double,而返回浮点值的大多数数学函数都将返回double。如果你只使用double,就可以省去很多麻烦和排版。
#8
5
I just ran into a error that took me forever to figure out and potentially can give you a good example of float precision.
我刚刚遇到了一个错误,这个错误让我花了很长时间才算出来,它可能会给你一个浮点精度的好例子。
#include <iostream>
#include <iomanip>
int main(){
for(float t=0;t<1;t+=0.01){
std::cout << std::fixed << std::setprecision(6) << t << std::endl;
}
}
The output is
输出是
0.000000
0.010000
0.020000
0.030000
0.040000
0.050000
0.060000
0.070000
0.080000
0.090000
0.100000
0.110000
0.120000
0.130000
0.140000
0.150000
0.160000
0.170000
0.180000
0.190000
0.200000
0.210000
0.220000
0.230000
0.240000
0.250000
0.260000
0.270000
0.280000
0.290000
0.300000
0.310000
0.320000
0.330000
0.340000
0.350000
0.360000
0.370000
0.380000
0.390000
0.400000
0.410000
0.420000
0.430000
0.440000
0.450000
0.460000
0.470000
0.480000
0.490000
0.500000
0.510000
0.520000
0.530000
0.540000
0.550000
0.560000
0.570000
0.580000
0.590000
0.600000
0.610000
0.620000
0.630000
0.640000
0.650000
0.660000
0.670000
0.680000
0.690000
0.700000
0.710000
0.720000
0.730000
0.740000
0.750000
0.760000
0.770000
0.780000
0.790000
0.800000
0.810000
0.820000
0.830000
0.839999
0.849999
0.859999
0.869999
0.879999
0.889999
0.899999
0.909999
0.919999
0.929999
0.939999
0.949999
0.959999
0.969999
0.979999
0.989999
0.999999
As you can see after 0.83, the precision runs down significantly.
正如您在0.83之后看到的,精度显著下降。
However, if I set up t
as double, such an issue won't happen.
但是,如果我把t设为2,这样的问题就不会发生。
It took me five hours to realize this minor error, which ruined my program.
我花了5个小时才意识到这个小错误,它毁了我的程序。
#9
3
When using floating point numbers you cannot trust that your local tests will be exactly the same as the tests that are done on the server side. The environment and the compiler are probably different on you local system and where the final tests are run. I have seen this problem many times before in some TopCoder competitions especially if you try to compare two floating point numbers.
当使用浮点数时,您不能相信您的本地测试将与服务器端的测试完全相同。环境和编译器在本地系统和运行最终测试的地方可能是不同的。我以前在一些*编码器比赛中见过很多次这个问题,尤其是当你试图比较两个浮点数的时候。
#10
2
The built-in comparison operations differ as in when you compare 2 numbers with floating point, the difference in data type (i.e. float or double) may result in different outcomes.
内置的比较操作不同于将两个数字与浮点数进行比较时,数据类型(例如浮点数或双精度数)的差异可能导致不同的结果。
#11
-1
Unlike an int
(whole number), a float
have a decimal point, and so can a double
. But the difference between the two is that a double
is twice as detailed as a float
, meaning that it can have double the amount of numbers after the decimal point.
与整数(整数)不同,浮点数有小数点,双精度浮点数也可以。但两者的不同之处在于,双精度浮点数的细节是浮点数的两倍,这意味着在小数点后,双精度浮点数的数量可以增加一倍。
#1
385
Huge difference.
巨大的差异。
As the name implies, a double
has 2x the precision of float
[1]. In general a double
has 15 decimal digits of precision, while float
has 7.
顾名思义,double的精度是float[1]的两倍。一般来说,双精度为15位,浮点数为7。
Here's how the number of digits are calculated:
下面是计算数字的方法:
double
has 52 mantissa bits + 1 hidden bit: log(253)÷log(10) = 15.95 digits双有52位+ 1隐位尾数:日志(253)÷日志(10)= 15.95位
float
has 23 mantissa bits + 1 hidden bit: log(224)÷log(10) = 7.22 digits浮动有23位+ 1隐位尾数:日志(224)÷日志(10)= 7.22位
This precision loss could lead to truncation errors much easier to float up, e.g.
这种精度损失可能导致截断误差更容易浮动,例如。
float a = 1.f / 81;
float b = 0;
for (int i = 0; i < 729; ++ i)
b += a;
printf("%.7g\n", b); // prints 9.000023
while
而
double a = 1.0 / 81;
double b = 0;
for (int i = 0; i < 729; ++ i)
b += a;
printf("%.15g\n", b); // prints 8.99999999999996
Also, the maximum value of float is about 3e38
, but double is about 1.7e308
, so using float
can hit "infinity" (i.e. a special floating-point number) much more easily than double
for something simple, e.g. computing the factorial of 60.
另外,float的最大值大约是3e38,而double大约是1.7e308,所以使用float比double更容易达到“infinity”(即一个特殊的浮点数),例如计算60的阶乘。
During testing, maybe a few test cases contain these huge numbers, which may cause your programs to fail if you use floats.
在测试期间,可能有几个测试用例包含这些巨大的数字,如果使用浮点数,可能会导致程序失败。
Of course, sometimes, even double
isn't accurate enough, hence we sometimes have long double
[1] (the above example gives 9.000000000000000066 on Mac), but all floating point types suffer from round-off errors, so if precision is very important (e.g. money processing) you should use int
or a fraction class.
当然,有时甚至连double都不够精确,因此有时我们有long double[1](上面的示例在Mac上给出了9.000000000000000000000000000066),但是所有浮点类型都有舍入错误,所以如果精度非常重要(例如货币处理),您应该使用int类或分数类。
Furthermore, don't use +=
to sum lots of floating point numbers, as the errors accumulate quickly. If you're using Python, use fsum
. Otherwise, try to implement the Kahan summation algorithm.
此外,不要使用+=和大量浮点数,因为错误会迅速累积。如果您正在使用Python,请使用fsum。否则,尝试实现Kahan求和算法。
[1]: The C and C++ standards do not specify the representation of float
, double
and long double
. It is possible that all three are implemented as IEEE double-precision. Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float
is indeed a IEEE single-precision floating point number (binary32), and double
is a IEEE double-precision floating point number (binary64).
[1]: C和c++标准没有指定浮点、双、长双的表示。这三种方法都可以实现为IEEE双精度。然而,对于大多数架构(gcc, MSVC;x86、x64、ARM) float实际上是IEEE单精度浮点数(binary32), double是IEEE双精度浮点数(binary64)。
#2
49
Here is what the standard C99 (ISO-IEC 9899 6.2.5 §10) or C++2003 (ISO-IEC 14882-2003 3.1.9 §8) standards say:
这就是C99标准(9899年ISO-IEC 6.2.5§10)或C + + 2003(ISO-IEC 14882 - 2003 3.1.9§8)标准说:
There are three floating point types:
float
,double
, andlong double
. The typedouble
provides at least as much precision asfloat
, and the typelong double
provides at least as much precision asdouble
. The set of values of the typefloat
is a subset of the set of values of the typedouble
; the set of values of the typedouble
is a subset of the set of values of the typelong double
.有三种浮点类型:浮点类型、双类型和长双类型。类型double提供的精度至少与浮点数相同,类型long double提供的精度至少与double相同。类型float的值集是类型double的值集的子集;类型double的值集是类型long double的值集的子集。
The C++ standard adds:
c++标准补充道:
The value representation of floating-point types is implementation-defined.
浮点类型的值表示是实现定义的。
I would suggest having a look at the excellent What Every Computer Scientist Should Know About Floating-Point Arithmetic that covers the IEEE floating-point standard in depth. You'll learn about the representation details and you'll realize there is a tradeoff between magnitude and precision. The precision of the floating point representation increases as the magnitude decreases, hence floating point numbers between -1 and 1 are those with the most precision.
我建议大家看看每一个计算机科学家都应该了解的关于浮点算法的优秀知识,这些算法深入地涵盖了IEEE浮点标准。您将了解表示细节,并认识到大小和精度之间存在权衡。浮点表示法的精度随着量值的减小而增大,因此在-1和1之间的浮点数是最精确的。
#3
24
Given a quadratic equation: x2 − 4.0000000 x + 3.9999999 = 0, the exact roots to 10 significant digits are, r1 = 2.000316228 and r2 = 1.999683772.
给定一个二次方程:x2−4.0000000 x + 3.9999999 = 0,精确到10根有效数字,r1 = 2.000316228和r2 = 1.999683772。
Using float
and double
, we can write a test program:
使用float和double,我们可以编写一个测试程序:
#include <stdio.h>
#include <math.h>
void dbl_solve(double a, double b, double c)
{
double d = b*b - 4.0*a*c;
double sd = sqrt(d);
double r1 = (-b + sd) / (2.0*a);
double r2 = (-b - sd) / (2.0*a);
printf("%.5f\t%.5f\n", r1, r2);
}
void flt_solve(float a, float b, float c)
{
float d = b*b - 4.0f*a*c;
float sd = sqrtf(d);
float r1 = (-b + sd) / (2.0f*a);
float r2 = (-b - sd) / (2.0f*a);
printf("%.5f\t%.5f\n", r1, r2);
}
int main(void)
{
float fa = 1.0f;
float fb = -4.0000000f;
float fc = 3.9999999f;
double da = 1.0;
double db = -4.0000000;
double dc = 3.9999999;
flt_solve(fa, fb, fc);
dbl_solve(da, db, dc);
return 0;
}
Running the program gives me:
运行这个程序给了我:
2.00000 2.00000
2.00032 1.99968
Note that the numbers aren't large, but still you get cancellation effects using float
.
注意,这些数字并不大,但是仍然可以使用float来获得取消效果。
(In fact, the above is not the best way of solving quadratic equations using either single- or double-precision floating-point numbers, but the answer remains unchanged even if one uses a more stable method.)
(事实上,上述方法并不是使用单精度浮点数或双精度浮点数求解二次方程的最佳方法,但即使使用更稳定的方法,其结果也不会改变。)
#4
17
- A double is 64 and single precision (float) is 32 bits.
- 双精度为64,单精度(浮点数)为32位。
- The double has a bigger mantissa (the integer bits of the real number).
- 双精度浮点数有一个较大的尾数(实数的整数位)。
- Any inaccuracies will be smaller in the double.
- 任何不准确的地方都将缩小。
#5
11
The size of the numbers involved in the float-point calculations is not the most relevant thing. It's the calculation that is being performed that is relevant.
浮点计算中涉及的数字的大小并不是最相关的。正在进行的计算是相关的。
In essence, if you're performing a calculation and the result is an irrational number or recurring decimal, then there will be rounding errors when that number is squashed into the finite size data structure you're using. Since double is twice the size of float then the rounding error will be a lot smaller.
本质上,如果您正在执行一个计算,结果是一个无理数或循环小数,那么当这个数字被压缩到您正在使用的有限大小的数据结构中时,将会出现四舍五入错误。由于double的大小是float大小的两倍,因此舍入误差会小得多。
The tests may specifically use numbers which would cause this kind of error and therefore tested that you'd used the appropriate type in your code.
测试可能会特别使用会导致这种错误的数字,因此测试您在代码中使用了适当的类型。
#6
8
Floats have less precision than doubles. Although you already know, read What WE Should Know About Floating-Point Arithmetic for better understanding.
浮点数的精度低于双精度数。尽管你已经知道了,为了更好的理解,阅读我们应该知道的浮点算术。
#7
8
Type float, 32 bits long, has a precision of 7 digits. While it may store values with very large or very small range (+/- 3.4 * 10^38 or * 10^-38), it has only 7 significant digits.
浮点类型,32位长,精度为7位。虽然存储值和非常大或非常小的范围(+ / - 3.4 * 10 ^ 38 * 10 ^ -38),它只有7位有效数字。
Type double, 64 bits long, has a bigger range (*10^+/-308) and 15 digits precision.
型双,64位长,有一个更大的范围(* 10 ^ + / -308)和15位精度。
Type long double is nominally 80 bits, though a given compiler/OS pairing may store it as 12-16 bytes for alignment purposes. The long double has an exponent that just ridiculously huge and should have 19 digits precision. Microsoft, in their infinite wisdom, limits long double to 8 bytes, the same as plain double.
长双字节在名义上是80位,但是给定的编译器/操作系统对可以将其存储为12-16字节以进行对齐。长倍的指数非常大,应该有19位数字的精度。微软拥有无限的智慧,将长双字节限制为8字节,就像普通双字节一样。
Generally speaking, just use type double when you need a floating point value/variable. Literal floating point values used in expressions will be treated as doubles by default, and most of the math functions that return floating point values return doubles. You'll save yourself many headaches and typecastings if you just use double.
一般来说,当需要浮点值/变量时,只需使用类型double。在表达式中使用的文字浮点值在默认情况下将被处理为double,而返回浮点值的大多数数学函数都将返回double。如果你只使用double,就可以省去很多麻烦和排版。
#8
5
I just ran into a error that took me forever to figure out and potentially can give you a good example of float precision.
我刚刚遇到了一个错误,这个错误让我花了很长时间才算出来,它可能会给你一个浮点精度的好例子。
#include <iostream>
#include <iomanip>
int main(){
for(float t=0;t<1;t+=0.01){
std::cout << std::fixed << std::setprecision(6) << t << std::endl;
}
}
The output is
输出是
0.000000
0.010000
0.020000
0.030000
0.040000
0.050000
0.060000
0.070000
0.080000
0.090000
0.100000
0.110000
0.120000
0.130000
0.140000
0.150000
0.160000
0.170000
0.180000
0.190000
0.200000
0.210000
0.220000
0.230000
0.240000
0.250000
0.260000
0.270000
0.280000
0.290000
0.300000
0.310000
0.320000
0.330000
0.340000
0.350000
0.360000
0.370000
0.380000
0.390000
0.400000
0.410000
0.420000
0.430000
0.440000
0.450000
0.460000
0.470000
0.480000
0.490000
0.500000
0.510000
0.520000
0.530000
0.540000
0.550000
0.560000
0.570000
0.580000
0.590000
0.600000
0.610000
0.620000
0.630000
0.640000
0.650000
0.660000
0.670000
0.680000
0.690000
0.700000
0.710000
0.720000
0.730000
0.740000
0.750000
0.760000
0.770000
0.780000
0.790000
0.800000
0.810000
0.820000
0.830000
0.839999
0.849999
0.859999
0.869999
0.879999
0.889999
0.899999
0.909999
0.919999
0.929999
0.939999
0.949999
0.959999
0.969999
0.979999
0.989999
0.999999
As you can see after 0.83, the precision runs down significantly.
正如您在0.83之后看到的,精度显著下降。
However, if I set up t
as double, such an issue won't happen.
但是,如果我把t设为2,这样的问题就不会发生。
It took me five hours to realize this minor error, which ruined my program.
我花了5个小时才意识到这个小错误,它毁了我的程序。
#9
3
When using floating point numbers you cannot trust that your local tests will be exactly the same as the tests that are done on the server side. The environment and the compiler are probably different on you local system and where the final tests are run. I have seen this problem many times before in some TopCoder competitions especially if you try to compare two floating point numbers.
当使用浮点数时,您不能相信您的本地测试将与服务器端的测试完全相同。环境和编译器在本地系统和运行最终测试的地方可能是不同的。我以前在一些*编码器比赛中见过很多次这个问题,尤其是当你试图比较两个浮点数的时候。
#10
2
The built-in comparison operations differ as in when you compare 2 numbers with floating point, the difference in data type (i.e. float or double) may result in different outcomes.
内置的比较操作不同于将两个数字与浮点数进行比较时,数据类型(例如浮点数或双精度数)的差异可能导致不同的结果。
#11
-1
Unlike an int
(whole number), a float
have a decimal point, and so can a double
. But the difference between the two is that a double
is twice as detailed as a float
, meaning that it can have double the amount of numbers after the decimal point.
与整数(整数)不同,浮点数有小数点,双精度浮点数也可以。但两者的不同之处在于,双精度浮点数的细节是浮点数的两倍,这意味着在小数点后,双精度浮点数的数量可以增加一倍。