ASP.使用浮点数的净转换
本文关键字:转换 浮点数 ASP | 更新日期: 2023-09-27 18:01:24
我在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
为什么等于0的fEstimateVal仍然在if循环中运行?
处理浮点数/双精度数总是精度平衡。如果要与clear 0进行比较,请将其转换为整数,然后再进行比较。
或者添加一些精确的东西,比如
((int)(floatnumber * 100)) == 0.
下面的代码可以正常工作:
if (zeroDataCell.fEstimateVal != 0)
zeroDataCell.fRse = zeroDataCell.fTwx / zeroDataCell.fEstimateVal * 100.0f;