使用浮点数的 ASP.Net 转换
我的 asp.net/C# 数学应用程序中存在转换或某种类型转换问题。承包商编写了一些代码来计算装配中的相对标准误差。但当我运行它时,显然无法正常工作。
数据类型如下:
public double dSwx = 0.0, dSw = 100.0;
public double dTx = 0.0, dTwn=0.0;
public float fEstimateVal = 0.0F; // For Estimate Value, it should be same as Swx/Sw
//i did a system.out on this in my web app and they came out as:
//zeroDataCell.dSwx = 0.0;
//zeroDataCell.dSw = 100.0;
zeroDataCell.fEstimateVal = (float)(zeroDataCell.dSwx / zeroDataCell.dSw) * 100.0f;
//so now zeroDataCell.fEstimateVal should be 0.0, but my code blows
//through the if statement below, is there some conversion problem?
//should i use EqualsTo?
if (zeroDataCell.fEstimateVal != 0.0f)
zeroDataCell.fRse = zeroDataCell.fTwx / zeroDataCell.fEstimateVal * 100.0f;0
为什么等于零的fEstimateVal仍然经过if循环?
I have a conversion or some kind of type casting problem in my asp.net/C# math app. A contractor wrote some code to calculate a relative standard error in an assembly. But when I run it, apparently doesn't work right.
The data types are as follows:
public double dSwx = 0.0, dSw = 100.0;
public double dTx = 0.0, dTwn=0.0;
public float fEstimateVal = 0.0F; // For Estimate Value, it should be same as Swx/Sw
//i did a system.out on this in my web app and they came out as:
//zeroDataCell.dSwx = 0.0;
//zeroDataCell.dSw = 100.0;
zeroDataCell.fEstimateVal = (float)(zeroDataCell.dSwx / zeroDataCell.dSw) * 100.0f;
//so now zeroDataCell.fEstimateVal should be 0.0, but my code blows
//through the if statement below, is there some conversion problem?
//should i use EqualsTo?
if (zeroDataCell.fEstimateVal != 0.0f)
zeroDataCell.fRse = zeroDataCell.fTwx / zeroDataCell.fEstimateVal * 100.0f;0
Why does fEstimateVal that eqauls zero stil run through the if loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理浮点/双精度数字始终是精度平衡。如果你想将它与清除 0 进行比较,请将其转换为整数然后进行比较。
或者添加一些精确的东西,比如
Dealing with float/double numbers is always precision balancing. If you want to compare it against clear 0, convert it to integer and compare after.
Or add some precisio stuff like
下面的代码在这里工作得很好:
The following code works fine here: