如何比较两个浮点数相等呢?

m0772 2005-02-28 12:36:05
我在程序中这么做f1==f2,可是明明相等但是程序中却按不相等的去做了。难道浮点数比较还有什么需要注意的问题吗?

...全文
1765 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyjzy5 2005-03-01
  • 打赏
  • 举报
回复
浮点运算后,结果在存入内存中会产生舍入,这很可能会带来误差,因此,我们应当尽量使用高精度的数据类型,比如用gcc的时候,我们尽量使用long double而非double、float,这会减少相当多的错误机会。作为一般的比较,你设置好差值精度再比较就行了。
tyzyx 2005-02-28
  • 打赏
  • 举报
回复
修正

f1-f2<FLT_MIN (MSDN 上给的标准值 1.175494351e-38F )
tyzyx 2005-02-28
  • 打赏
  • 举报
回复
Data Type Constants
Remarks

Data type constants are implementation-dependent ranges of values allowed for integral data types. The constants listed below give the ranges for the integral data types and are defined in LIMITS.H.

Note The /J compiler option changes the default char type to unsigned.

Constant Value Meaning
SCHAR_MAX 127 Maximum signed char value
SCHAR_MIN –128 Minimum signed char value
UCHAR_MAX 255
(0xff) Maximum unsigned char value
CHAR_BIT 8 Number of bits in a char
USHRT_MAX 65535
(0xffff) Maximum unsigned short value
SHRT_MAX 32767 Maximum (signed) short value
SHRT_MIN –32768 Minimum (signed) short value
UINT_MAX 4294967295
(0xffffffff) Maximum unsigned int value
ULONG_MAX 4294967295
(0xffffffff) Maximum unsigned long value
INT_MAX 2147483647 Maximum (signed) int value
INT_MIN –2147483647–1 Minimum (signed) int value
LONG_MAX 2147483647 Maximum (signed) long value
LONG_MIN –2147483647–1 Minimum (signed) long value
CHAR_MAX 127
(255 if /J option used) Maximum char value
CHAR_MIN –128
(0 if /J option used) Minimum char value
MB_LEN_MAX 2 Maximum number of bytes in multibyte char


The following constants give the range and other characteristics of the double and float data types, and are defined in FLOAT.H:

Constant Value Description
DBL_DIG 15 # of decimal digits of precision
DBL_EPSILON 2.2204460492503131e-016 Smallest such that 1.0+DBL_EPSILON !=1.0
DBL_MANT_DIG 53 # of bits in mantissa
DBL_MAX 1.7976931348623158e+308 Maximum value
DBL_MAX_10_EXP 308 Maximum decimal exponent
DBL_MAX_EXP 1024 Maximum binary exponent
DBL_MIN 2.2250738585072014e-308 Minimum positive value
DBL_MIN_10_EXP (-307) Minimum decimal exponent
DBL_MIN_EXP (–1021) Minimum binary exponent
_DBL_RADIX 2 Exponent radix
_DBL_ROUNDS 1 Addition rounding: near
FLT_DIG 6 Number of decimal digits of precision
FLT_EPSILON 1.192092896e-07F Smallest such that 1.0+FLT_EPSILON !=1.0
FLT_MANT_DIG 24 Number of bits in mantissa
FLT_MAX 3.402823466e+38F Maximum value
FLT_MAX_10_EXP 38 Maximum decimal exponent
FLT_MAX_EXP 128 Maximum binary exponent
FLT_MIN 1.175494351e-38F Minimum positive value
FLT_MIN_10_EXP (–37) Minimum decimal exponent
FLT_MIN_EXP (–125) Minimum binary exponent
FLT_RADIX 2 Exponent radix
FLT_ROUNDS 1 Addition rounding: near
tyzyx 2005-02-28
  • 打赏
  • 举报
回复
正确答案

f1-f2<DBL_MIN (MSDN 上给的标准值 2.2250738585072014e-308 )
DragonBill 2005-02-28
  • 打赏
  • 举报
回复
if( fabs(f1-f2) <0.000001
gietwgh 2005-02-28
  • 打赏
  • 举报
回复
呵呵,看来基础不过关
看看林锐的《高质量C++编程》吧
浮点数的比较不能简单的用==
而要判断精度
he_sl(he_sl) 正解!!
pantion 2005-02-28
  • 打赏
  • 举报
回复
定义一个宏 #define PAN 0.000001;然后就可以比较了if(f1-f2<=PAN),或者if(f1-f2>=PAN)
johnshao 2005-02-28
  • 打赏
  • 举报
回复
其中的精度可调,看你的需要。
老夏Max 2005-02-28
  • 打赏
  • 举报
回复
通过相减的差值和约定的精度相比较。楼上正解
legendhui 2005-02-28
  • 打赏
  • 举报
回复
if( fabs(f1-f2) <0.000001
oyljerry 2005-02-28
  • 打赏
  • 举报
回复
通过相减来判断误差范围
he_sl 2005-02-28
  • 打赏
  • 举报
回复
if( fabs(f1-f2) < 预先指定的精度)
{
...
}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧