简体   繁体   English

比较浮点舍入失败System.Math.RoundTo C ++ XE7

[英]comparison float rounding fails System.Math.RoundTo C++ XE7

I've been trying to round a float value to 4 precision without success. 我一直在尝试将浮点值四舍五入到4个精度,但没有成功。

float fconv = 1.0f;
float fdata = 39.934543423412f;
float fres = RoundTo(fdata*fconv, -4);

if(fres <= 39.9345f){do something;} //<-- unwanted behavior

Wanted result is 39.934500000000 想要的结果是39.934500000000
Actual result is 39.934543423412 实际结果是39.934543423412

I've tried many methods including Round a float to a given precision without success. 我尝试了许多方法,包括将浮点数舍入到给定的精度没有成功。

I'm working on an AMD FX83xx 64bit. 我正在使用AMD FX83xx 64位。 Program is built in 32bit Debug using XE7 程序使用XE7内置于32位调试中

Thanks 谢谢

Your desired precision of 6 decimal digits is very near the precision limits for a float data type. 您希望的6个十进制数字的精度非常接近float数据类型的精度极限。 The epsilon, or delta between consecutive representable floating point values, for a number around 40f is about 7.63E-6, so there's only a couple of bits different between the 'best' value and what you're getting. 对于40f左右的数字,ε或连续可表示的浮点值之间的增量约为7.63E-6,因此“最佳”值与所得到的值之间只有几位不同。 This is possibly due to rounding that close to the limit, but I'm not sure. 这可能是由于四舍五入接近极限,但我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM