C编译器的bug(浮点运算)? [英] C compiler bug (floating point arithmetic)?

查看:98
本文介绍了C编译器的bug(浮点运算)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>

int main()
{
    double fract=0;
    int tmp;

    //scanf("%lf",&fract);
    fract=0.312;
    printf("%lf",fract);
    printf("\n\n");
    while(fract>0){
        fract*=(double)10;
        printf("%d ",(int)fract);
        fract-=(int)fract;
    }
    getch();
    return 0;
}

这code具有768,16的输出:
312

this code shoud have an output of: 312

但somehing不会正确的..我使用DEVCPP 4.9.9.2编译器...

but somehing isn't going right.. i'm using devcpp 4.9.9.2 compiler...

推荐答案

&Kernighan的放大器; Plauger说,在他们老了,但经典的书 ,即编程风格的要素:

Kernighan & Plauger say, in their old but classic book "The Elements of Programming Style", that:


  • 一个聪明的老程序员曾经说过:浮点数像颗颗小堆,每移动一次,你失去了一点沙子,并获得一点点灰尘。

他们还说:


  • 10 * 0.1是未落1.0

这两种说法指出,浮点运算是不是precise。

Both sayings point out that floating point arithmetic is not precise.

请注意,一些现代的CPU(IBM PPC)具有IEEE 754:2008十进制浮点运算内置。如果使用正确的类型,那么你的计算将是准确的。

Note that some modern CPUs (IBM PPC) have IEEE 754:2008 decimal floating point arithmetic built-in. If using the correct types, then your calculation will be exact.

这篇关于C编译器的bug(浮点运算)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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