浮动溢出? [英] float overflow?

查看:110
本文介绍了浮动溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code似乎总是产生错误的结果。我已经测试了gcc和窗户视觉工作室。难道是因为浮动溢出或别的东西吗?在此先感谢:)

 的#include<&stdio.h中GT;
#定义ñ51200000
诠释的main()
{
 浮动F = 0.0;
 的for(int i = 0; I< N;我++)
  F + = 1.0F;
 fprintf中(标准输出,%F \\ N,F);
 返回0;
}


解决方案

浮动只有precision 23位。 5.12亿需要26.简单地说,你没有一个正确答案所需的precision。

The following code seems to always generate wrong result. I have tested it on gcc and windows visual studio. Is it because of float overflow or something else? Thanks in advance:)

#include <stdio.h>
#define N 51200000
int main()
{
 float f = 0.0f;
 for(int i = 0; i < N; i++)
  f += 1.0f;
 fprintf(stdout, "%f\n", f);
 return 0;
}

解决方案

float only has 23 bits of precision. 512000000 requires 26. Simply put, you do not have the precision required for a correct answer.

这篇关于浮动溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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