C# 中 frac 函数的最快实现
我想在 C# 中实现一个 frac 函数(就像 hsl 中的函数一样 http://msdn.microsoft.com/en-us/library/bb509603%28VS.85%29.aspx),但由于它是针对处理器密集型应用程序,我希望可能的最佳版本。我正在使用类似的东西
public float Frac(float value)
{
return value - (float)Math.Truncate(value);
}
,但我遇到了精度问题,例如对于 2.6f,它在单元测试中返回
预期:0.600000024f 但为:0.599999905f
我知道我可以将值转换为十进制,然后最后转换为float 以获得正确的结果,如下所示:
public float Frac(float value)
{
return (float)((decimal)value - Decimal.Truncate((decimal)value));
}
但我想知道是否有更好的方法而不诉诸小数......
I would like to implement a frac function in C# (just like the one in hsl here http://msdn.microsoft.com/en-us/library/bb509603%28VS.85%29.aspx) but since it is for a very processor intensive application i would like the best version possible. I was using something like
public float Frac(float value)
{
return value - (float)Math.Truncate(value);
}
but I'm having precision problems, for example for 2.6f it's returning in the unit test
Expected: 0.600000024f But was: 0.599999905f
I know that I can convert to decimal the value and then at the end convert to float to obtain the correct result something like this:
public float Frac(float value)
{
return (float)((decimal)value - Decimal.Truncate((decimal)value));
}
But I wonder if there is a better way without resorting to decimals...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
float(System.Single 的别名)仅精确到 7数字。无论如何,您不应该真正使用相等来比较浮点值。您应该检查它们是否在可容忍的范围内 - 例如,预期值和实际值之间的差异不超过 0.000001。
如果您使用 double 代替,您将精确到 15-16数字。
float (an alias for System.Single) is only precise to 7 digits. You shouldn't really compare floating point values using equality anyway. You should instead check that they are within a tolerable range - for example that the difference between the expected and actual values is not more than 0.000001.
If you use double instead, you will be precise to 15-16 digits.
您将需要使用小数,如下所示:
因此,当您超过 2.6m 时,您将始终得到 0.6
You are going to need to use decimals as in:
So, when you pass 2.6m you will always get .6