[英]Why float operation return integer numbers in C# PointF?
I have the following statement which uses all flat data type.我有以下使用所有平面数据类型的语句。
ScaledCordinates.BottomRight = new PointF(((float)ScaleBottomRight.X * (float)Scale), ((float)ScaleBottomRight.Y * (float)Scale));
The values assigned to "ScaleBottomRight.X" is 2.36523485
and "ScaleBottomRight.Y" is 2.38020468
.分配给
"ScaleBottomRight.X" is 2.36523485
, "ScaleBottomRight.Y" is 2.38020468
。 "Scale" = 66.80098
. "Scale" = 66.80098
。
I don't understand why "ScaledCordinates.BottomRight"
is giving me {X = 158 Y = 159}
in my app.我不明白为什么
"ScaledCordinates.BottomRight"
在我的应用程序中给了我{X = 158 Y = 159}
。 Why is the float operation returns integer values?为什么浮点运算返回整数值? It's even frustrating some times output is an integer and some times float.
有时甚至令人沮丧的是输出是整数,有时是浮点数。 I did all sorts of conversions between double and float but the same result.
我在 double 和 float 之间进行了各种转换,但结果相同。 What's the best way to fix these issues?
解决这些问题的最佳方法是什么? The output should be a "float" data type.
输出应该是“浮动”数据类型。
2.36523485 * 66.80098 = 158.000005910153
and和
2.38020468 * 66.80098 = 159.0000052245864
They both have too small fractional part to fit in float precision, which is ~6-9 digits according to the docs .它们的小数部分都太小,无法适应浮点精度,根据docs约为 6-9 位。 The result of multiplication is still
float
(ie System.Single
) though:乘法的结果仍然是
float
(即System.Single
):
Console.WriteLine((2.36523485f * 66.80098f)); // prints "158"
Console.WriteLine((2.36523485f * 66.80098f).GetType()) // prints "System.Single"
I would assume that whatever Type ScaledCordinates.BottomRight
is, its X
and Y
properties (of fields) are of Type int
.我会假设无论
ScaledCordinates.BottomRight
类型是什么,它的X
和Y
属性(字段)都是int
类型。
For example, it might be of Type Point , whose X and Y are int
s: X docs .例如,它可能是类型Point ,其 X 和 Y 是
int
s: X docs 。 Y docs . Y 文档。
You can change it to a different Type.您可以将其更改为不同的类型。 But as long as they are
int
s - you can't keep the floating point information.但是只要它们是
int
s - 你就不能保留浮点信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.