相关疑难解决方法(0)

为什么C#中的浮点运算不精确?

为什么以下程序会打印出打印的内容?

class Program
{
    static void Main(string[] args)
    {
        float f1 = 0.09f*100f;
        float f2 = 0.09f*99.999999f;

        Console.WriteLine(f1 > f2);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是

false
Run Code Online (Sandbox Code Playgroud)

c# floating-point

13
推荐指数
3
解决办法
2万
查看次数

为什么ghci说1.1 + 1.1 + 1.1> 3.3是真的?

我最近经历了一个Haskell教程,并在交互式ghcishell中尝试一些简单的Haskell表达式时注意到了这种行为:

Prelude> 1.1 + 1.1 == 2.2
True
Prelude> 1.1 + 1.1 + 1.1 == 3.3
False
Prelude> 1.1 + 1.1 + 1.1 > 3.3
True
Prelude> 1.1 + 1.1 + 1.1
3.3000000000000003
Run Code Online (Sandbox Code Playgroud)

有谁知道那是为什么?

haskell floating-accuracy ghci

8
推荐指数
6
解决办法
1246
查看次数

Perl for loop to haywire

我在Perl中有一个简单的for循环

for ($i=0; $i <= 360; $i += 0.01)
{
print "$i ";
}
Run Code Online (Sandbox Code Playgroud)

为什么当我运行这段代码时,我得到以下输出,一旦达到0.81,它突然开始加载更多的小数位?我知道我可以简单地围绕以避免这个问题,但我想知道它为什么会发生.0.01的增量似乎并不疯狂.

 0.77
 0.78
 0.79
 0.8
 0.81
 0.820000000000001
 0.830000000000001
 0.840000000000001
 0.850000000000001
 0.860000000000001
 0.870000000000001
Run Code Online (Sandbox Code Playgroud)

perl for-loop

5
推荐指数
2
解决办法
662
查看次数

在Perl中,为什么100*18.35不等于1835?

当我运行以下Perl单线程时:

$ perl -e 'print "Oh no!\n" unless 1835 == 100*18.35'
Run Code Online (Sandbox Code Playgroud)

我明白了

Oh no!

这是为什么?

floating-point perl floating-point-comparison

-2
推荐指数
1
解决办法
63
查看次数