为什么除法结果因转换类型而异? [英] Why does a division result differ based on the cast type?

查看:47
本文介绍了为什么除法结果因转换类型而异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我不明白的部分代码:

Here's a part of code that I dont understand:

byte b1 = (byte)(64 / 0.8f); // b1 is 79
int b2 = (int)(64 / 0.8f); // b2 is 79
float fl = (64 / 0.8f); // fl is 80

为什么前两个计算结果相差一个?我应该如何执行此操作,以便快速且正确?

Why are the first two calculations off by one? How should I perform this operation, so its fast and correct?

我需要以字节为单位的结果

I would need the result in byte

推荐答案

不完全正确,请参阅:为什么除法结果会因类型转换而不同?(后续)

四舍五入问题:通过转换为字节/整数,您正在裁剪小数位.

Rounding issue: By converting to byte / int, you are clipping of the decimal places.

但是 64/0.8 不应该导致任何小数位?错误:由于浮点数的性质,0.8f 不能完全像内存中那样表示;它存储为接近 0.8f 的值(但不完全是).请参阅浮点不准确示例或类似主题.因此,计算的结果不是 80.0f,而是 79.xxx,其中 xxx 接近 1 但仍不完全是 1.

But 64 / 0.8 should not result in any decimal places? Wrong: Due to the nature of floating point numbers, 0.8f can not be represented exactly like that in memory; it is stored as something close to 0.8f (but not exactly). See Floating point inaccuracy examples or similar threads. Thus, the result of the calculation is not 80.0f, but 79.xxx where xxx is close to 1 but still not exactly one.

您可以通过在 Visual Studio 的立即窗口中键入以下内容来验证这一点:

You can verify this by typing the following into the Immediate Window in Visual Studio:

(64 / 0.8f)
80.0
(64 / 0.8f) - 80
-0.0000011920929
100 * 0.8f - 80
0.0000011920929

您可以使用四舍五入来解决这个问题:

You can solve this by using rounding:

byte b1 = (byte)(64 / 0.8f + 0.5f);
int b2 = (int)(64 / 0.8f + 0.5f);
float fl = (64 / 0.8f);

这篇关于为什么除法结果因转换类型而异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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