使用浮点数的 ASP.Net 转换

发布于 2024-11-19 00:11:33 字数 925 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌 2024-11-26 00:11:33

处理浮点/双精度数字始终是精度平衡。如果你想将它与清除 0 进行比较,请将其转换为整数然后进行比较。

或者添加一些精确的东西,比如

((int)(floatnumber * 100)) == 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

((int)(floatnumber * 100)) == 0.
败给现实 2024-11-26 00:11:33

下面的代码在这里工作得很好:

if (zeroDataCell.fEstimateVal != 0)
  zeroDataCell.fRse = zeroDataCell.fTwx / zeroDataCell.fEstimateVal * 100.0f;

The following code works fine here:

if (zeroDataCell.fEstimateVal != 0)
  zeroDataCell.fRse = zeroDataCell.fTwx / zeroDataCell.fEstimateVal * 100.0f;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文