为什么浮点比较在不同的编译器上给出不同的输出结果? [英] Why floating point comparisons gives different outputs on different compiler?

查看:187
本文介绍了为什么浮点比较在不同的编译器上给出不同的输出结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读


IEEE标准将异常分成5类:溢出,下溢,除零,操作无效而不精确。每类异常都有一个单独的状态标志。前三个例外的含义是不言而喻的。无效操作覆盖表D-3中列出的情况,以及涉及NaN的任何比较。



I was reading this. It contains following C program.

#include<stdio.h>
int main()
{
    float x = 0.1;
    if (x == 0.1)
        printf("IF");
    else if (x == 0.1f)
        printf("ELSE IF");
    else
        printf("ELSE");
}

The article says that

The output of above program is "ELSE IF" which means the expression "x == 0.1″ returns false and expression "x == 0.1f" returns true.

But I tried it on different compilers & getting different outputs:

Here is the outputs on various IDEs.

1) Orwell Dev C++: ELSE

2) Code Blocks 13.12: ELSE IF but it gives following warnings during compilation.

warning: comparing floating point with == or != is unsafe.

Why this comparison is unsafe?

3) Ideone.com: ELSE IF (see run: http://ideone.com/VOE3E0)

4) TDM GCC 32 bit: ELSE IF

5) MSVS 2010: ELSE IF but compiles with warning

Warning 1 warning C4305: 'initializing' : truncation from 'double' to 'float'

What is exactly happening here? What's wrong with the program? Is it implementation defined behavior occurring?

Please help me.

解决方案

A floating point number maybe represented in the following form:

[sign] [mantissa] * 2[exponent]

So there will be rounding or relative errors when the space is less in memory.

From wiki:

Single-precision floating-point format is a computer number format that occupies 4 bytes (32 bits) in computer memory and represents a wide dynamic range of values by using a floating point.

The IEEE 754 standard specifies a binary32 as having:

Sign bit: 1 bit
Exponent width: 8 bits
Significand precision: 24 bits (23 explicitly stored)

This gives from 6 to 9 significant decimal digits precision (if a decimal string with at most 6 significant decimal is converted to IEEE 754 single precision and then converted back to the same number of significant decimal, then the final string should match the original; and if an IEEE 754 single precision is converted to a decimal string with at least 9 significant decimal and then converted back to single, then the final number must match the original [4]).

Larger (more bits) floating point representations allow for greater precision.

Floating point math is not exact. Simple values like 0.1 cannot be precisely represented using binary floating point numbers, and the limited precision of floating point numbers means that slight changes in the order of operations can change the result. A must read:

What Every Computer Scientist Should Know About Floating-Point Arithmetic

The IEEE standard divides exceptions into 5 classes: overflow, underflow, division by zero, invalid operation and inexact. There is a separate status flag for each class of exception. The meaning of the first three exceptions is self-evident. Invalid operation covers the situations listed in TABLE D-3, and any comparison that involves a NaN.

这篇关于为什么浮点比较在不同的编译器上给出不同的输出结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆