C糟透了数学? (救命) [英] C sucks at math? (help)

查看:58
本文介绍了C糟透了数学? (救命)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个小C程序,产生以下输出:

227.000000 / 5 = 45.400002


这里是代码:

int main(int argc,char * argv []){

float var = 227;

printf("%f / 5 =% f\ n,var,var / 5);

返回0;

}


显然,输出应该是是45.4(或45.400000),但对于一些

神秘的原因C决定在结果中添加0.000002 *。错误

可能看起来很小,这个程序显然是微不足道的,但是我需要在一个更大的项目中进行类似的计算和问题我是

遭遇是由于这个错误引起的。


*似乎不是.000002被添加了,而是

..0000015258789076710855 ...(我从结果中减去45.4

之后发现,然后乘以10,000,000,000,000,000)


为什么C会这样做,我能做什么?怎么办?


非常感谢任何帮助或提示!

I have made a little C program that generates the following output:
227.000000 / 5 = 45.400002

Here''s the code:
int main (int argc, char* argv[]) {
float var = 227;
printf("%f / 5 = %f\n", var, var / 5);
return 0;
}

Obviously, the output should be 45.4 (or 45.400000), but for some
mysterious reason C decided to add 0.000002* to the result. The error
might seem minor and this program is obviously trivial, but I need to
do similar calculations in a much bigger project and the problem I''m
encountering is caused by exactly this error.

* It seems that it is not .000002 is added, but rather
..0000015258789076710855... (which I found out after subtracting 45.4
from the result and then multiplying it with 10,000,000,000,000,000)

Why does C do this and what can I do about it?

Any help or tips are greatly appreciated!

推荐答案

Jordi写道:
我做了一个小C程序,生成以下输出:
227.000000 / 5 = 45.400002

这里是代码:
int main(int argc,char * argv []){
float var = 227;
printf("%f / 5 =%f \ n",var,var / 5);
返回0;
}

显然,输出应该是45.4(或45.400000),但是对于一些神秘的原因,C决定在结果中添加0.000002 *。错误
可能看起来很小,这个程序显然是微不足道的,但我需要在一个更大的项目中进行类似的计算,而我遇到的问题恰恰是由于这个错误引起的。

*似乎不是.000002被添加了,而是
..0000015258789076710855 ...(我从结果中减去45.4后发现了,然后将它乘以10,000,000,000,000,000)

为什么C会这样做以及我该怎么做呢?
I have made a little C program that generates the following output:
227.000000 / 5 = 45.400002

Here''s the code:
int main (int argc, char* argv[]) {
float var = 227;
printf("%f / 5 = %f\n", var, var / 5);
return 0;
}

Obviously, the output should be 45.4 (or 45.400000), but for some
mysterious reason C decided to add 0.000002* to the result. The error
might seem minor and this program is obviously trivial, but I need to
do similar calculations in a much bigger project and the problem I''m
encountering is caused by exactly this error.

* It seems that it is not .000002 is added, but rather
..0000015258789076710855... (which I found out after subtracting 45.4
from the result and then multiplying it with 10,000,000,000,000,000)

Why does C do this and what can I do about it?



它不是浮点硬件或仿真确实如此。


浮点并不准确。避免浮动和使用双打,即便如此,

你必须了解浮点数学的局限性。


你必须有一个狡猾的FPU,结果是在我的系统上45.400000 :)


-

Ian Collins。


It doesn''t, the floating point hardware or emulation does.

Floating point isn''t precise. Avoid floats and use doubles, even then,
you have to understand the limitations of floating point math.

You must have a dodgy FPU, the result is 45.400000 on my system :)

--
Ian Collins.


在文章< 11*********************@i40g2000cwc.googlegroups。 com>,

Jordi< jb ***** @ gmail.com>写道:
In article <11*********************@i40g2000cwc.googlegroups. com>,
Jordi <jb*****@gmail.com> wrote:
我做了一个小C程序,生成以下输出:
227.000000 / 5 = 45.400002
这里是代码:
int main( int argc,char * argv []){
float var = 227;
printf("%f / 5 =%f \ n",var,var / 5);
返回0;
}
I have made a little C program that generates the following output:
227.000000 / 5 = 45.400002 Here''s the code:
int main (int argc, char* argv[]) {
float var = 227;
printf("%f / 5 = %f\n", var, var / 5);
return 0;
}




您的程序使用未定义的行为:您没有原型

for printf()的范围。


[不可否认,修复可能不会解决你的困难;-)]

-

"重要的是要记住,在法律方面,计算机不会制作副本,只有人类才能制作副本。计算机给出了

命令,而非许可。只有人才能获得许可。

- Brad Templeton



Your program uses undefined behaviour: you do not have a prototype
for printf() in scope.

[Admittedly, fixing that probably won''t solve your difficulty ;-) ]
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton


感谢您的回复!


我刚刚发现仿真/铸造就是它的地方

错了,我正准备发布它。我可能不会想到

很快就会使用双打(我认为它们不存在于C和

中它们是用C ++引入的,我通常使用,或者别的什么,

感谢上帝我错了:P)。

似乎双打为玩具程序工作,所以他们可能会

整个项目,但它有点大,所以我不会改变它现在是b $ b(现在是凌晨2点36分)。 br />

顺便说一下,我正在使用SSH程序登录一些运行该软件的旧的Macintosh

计算机,所以很可能是

被认为是''狡猾''。 :)


无论如何,谢谢!

Ian Collins写道:
Thanks for your reply!

I had just figured out that the emulation/casting was where it went
wrong and I was about to post it. I would probably not have thought of
using doubles anytime soon though (I thought they didn''t exist in C and
that they were introduced with C++, which I normally use, or something,
thank God I''m wrong :P).
It seems that doubles work for the toy-program, so they probably will
for the entire project, but it''s kind of big so I won''t be altering it
right now (it''s 2:36 am here).

By the way, I''m using an SSH program to log in on some old Macintosh
computer where the software is running, so that could probably be
considered ''dodgy''. :)

Anyway, thanks!
Ian Collins wrote:
Jordi写道:
I已经制作了一个生成以下输出的小程序:
227.000000 / 5 = 45.400002

这里是代码:
int main(int argc,char * argv [ ]){
float var = 227;
printf("%f / 5 =%f \ n",var,var / 5);
返回0;
显然,输出应该是45.4(或45.400000),但是对于一些神秘的原因,C决定在结果中添加0.000002 *。错误
可能看起来很小,这个程序显然是微不足道的,但我需要在一个更大的项目中进行类似的计算,而我遇到的问题恰恰是由于这个错误引起的。

*似乎不是.000002被添加了,而是
..0000015258789076710855 ...(我从结果中减去45.4后发现了,然后将它乘以10,000,000,000,000,000)

为什么C会这样做以及我该怎么做呢?
I have made a little C program that generates the following output:
227.000000 / 5 = 45.400002

Here''s the code:
int main (int argc, char* argv[]) {
float var = 227;
printf("%f / 5 = %f\n", var, var / 5);
return 0;
}

Obviously, the output should be 45.4 (or 45.400000), but for some
mysterious reason C decided to add 0.000002* to the result. The error
might seem minor and this program is obviously trivial, but I need to
do similar calculations in a much bigger project and the problem I''m
encountering is caused by exactly this error.

* It seems that it is not .000002 is added, but rather
..0000015258789076710855... (which I found out after subtracting 45.4
from the result and then multiplying it with 10,000,000,000,000,000)

Why does C do this and what can I do about it?


它不是浮点硬件或仿真所做的。 />
浮点并不准确。避免浮动和使用双打,即使这样,你必须了解浮点数学的局限性。

你必须有一个狡猾的FPU,我的系统的结果是45.400000 :) />
-
Ian Collins。


It doesn''t, the floating point hardware or emulation does.

Floating point isn''t precise. Avoid floats and use doubles, even then,
you have to understand the limitations of floating point math.

You must have a dodgy FPU, the result is 45.400000 on my system :)

--
Ian Collins.






这篇关于C糟透了数学? (救命)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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