舍入错误从float转换为int [英] Rounding error casting from float to int
问题描述
大家好,我刚刚发现了C#中发生的舍入错误。我很确定
这是一个老问题,但这对我来说是新的,并且导致了相当多的时间...... b $ b时间试图追踪这个问题。 />
基本上将以下代码放入C#应用程序:
float testFloat2 =(int)(4.2f *(float)100);
Console.Out.WriteLine(" 1:" + testFloat2);
,结果将是419
如果我使用Convert.ToInt32我可以解决这个问题 - 但似乎
两者都应该做同样的事情,不应该吗?
新手
Hi all, I just discovered a rounding error that occurs in C#. I''m sure
this is an old issue, but it is new to me and resulted in a fair amount
of time trying to track down the issue.
Basically put the following code into your C# app:
float testFloat2 = (int) (4.2f * (float)100);
Console.Out.WriteLine("1: "+testFloat2);
and the result will be 419
If I use Convert.ToInt32 I can get around this problem - but it seems
that both should do the same thing, shouldn''t they?
Novice
推荐答案
实际上,它根本不是C#问题。浮点数很少是精确的
。所以4.2实际上就像4.199999999。结果
你看。
Bob
< 6t ** @ qlink.queensu.ca>在消息中写道
news:11 ********************* @ u72g2000cwu.googlegro ups.com ...
Actually, it''s not a C# problem at all. Floating point numbers are
rarely exact. So 4.2 is actually something like 4.199999999. Thus the result
you see.
Bob
<6t**@qlink.queensu.ca> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
大家好,我刚刚发现了C#中出现的舍入错误。我很确定这是一个老问题,但这对我来说是新的,导致了相当多的时间试图追查这个问题。
基本上把以下代码放入C#app:
float testFloat2 =(int)(4.2f *(float)100);
Console.Out.WriteLine(" 1:" + testFloat2);
如果我使用Convert.ToInt32我可以解决这个问题 - 但似乎两个都应该做同样的事情,不应该'他们?
新手
Hi all, I just discovered a rounding error that occurs in C#. I''m sure
this is an old issue, but it is new to me and resulted in a fair amount
of time trying to track down the issue.
Basically put the following code into your C# app:
float testFloat2 = (int) (4.2f * (float)100);
Console.Out.WriteLine("1: "+testFloat2);
and the result will be 419
If I use Convert.ToInt32 I can get around this problem - but it seems
that both should do the same thing, shouldn''t they?
Novice
6t ** @ qlink.queensu.ca 写道:
大家好,我刚刚发现了C#中发生的舍入错误。我很确定这是一个老问题,但这对我来说是新的,导致了相当多的时间试图追查这个问题。
基本上把以下代码放入C#app:
float testFloat2 =(int)(4.2f *(float)100);
Console.Out.WriteLine(" 1:" + testFloat2);
如果我使用Convert.ToInt32我可以解决这个问题 - 但似乎两个都应该做同样的事情,不应该'他们?
新手
Hi all, I just discovered a rounding error that occurs in C#. I''m sure
this is an old issue, but it is new to me and resulted in a fair amount
of time trying to track down the issue.
Basically put the following code into your C# app:
float testFloat2 = (int) (4.2f * (float)100);
Console.Out.WriteLine("1: "+testFloat2);
and the result will be 419
If I use Convert.ToInt32 I can get around this problem - but it seems
that both should do the same thing, shouldn''t they?
Novice
http://www.math.grin.edu/~stone/cour...EEE-reals.html
http://www.math.grin.edu/~stone/cour...EEE-reals.html
没有冒犯海报(鲍勃和KBuser),但你还没有真正回答我的问题。也许我可以把它说得好一点。
我更感兴趣的是为什么我从两个特定于.NET的不同方法得到不同的结果。我确定我会得到结果如果
我写了等效的VB.NET - 但我碰巧用C#编程。
也许,我不是在我原来的帖子中已经足够清楚,如果我使用
Convert.ToInt32然后我得到420,而不是419.所以很明显转换
方法正在做一些事情结果更准确。
无论如何,如果有人知道我为什么得到不同的结果,请随意发布
发布它。
谢谢,
新手
No offense to the posters (Bob and KBuser), but you haven''t really
answered my question. Perhaps I could have phrased it a little better.
I was more interested in why I get a different result from the two
different methods specific to .NET. I''m sure I would get the result if
I wrote equivalent VB.NET - but I happen to program in C#.
Perhaps, I wasn''t being clear enough in my original post, if I use
Convert.ToInt32 then I get 420, not 419. So obviously the Convert
method is doing something to make the result more accurate.
Anyway, if anyone knows why I get a different result please feel free
to post it.
Thanks,
Novice
这篇关于舍入错误从float转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!