gpt4 book ai didi

.net - System.Math.Round 错误?

转载 作者:行者123 更新时间:2023-12-02 15:44:10 26 4
gpt4 key购买 nike

我正在编写一个将数字四舍五入到两位的函数。当我尝试舍入特定值时,我发现了一个错误。所以,我运行了代码:

class Program {
static void Main(string[] args) {
int limit = 100;

for (int number = 0; number <= limit; number++) {
Console.WriteLine((System.Math.Round((double)(number+0.995),2,MidpointRounding.AwayFromZero)));
}
}
}

我发现:8.999.99 11121314151617 号18192021222324252627282930313232.9933.9934.9935.9936.9937.9938.9939.99

数字不会四舍五入到下一个值。

当我运行相同的代码直到 1500 时:我得到数字:

8.999.9932.9933.9934.9935.9936.9937.9938.9939.991024.991025.991026.991027.991028.991029.991030.991031.991032.991033.991034.991035.991036.991037.991038.991039.991040.991041.991042.991043.991044.991045.991046.991047.991048.991049.991050.991051.991052.991053.991054.991055.991056.991057.991058.991059.991060.991061.991062.991063.991064.991065.991066.991067.991068.991069.991070.991071.991072.991073.991074.991075.991076.991077.991078.991079.991080.991081.991082.991083.991084.991085.991086.991087.991088.991089.991090.991091.991092.991093.991094.991095.991096.991097.991098.991099.991100.991101.991102.991103.991104.991105.991106.991107.991108.991109.991110.991111.991112.991113.991114.991115.991116.991117.991118.991119.991120.991121.991122.991123.991124.991125.991126.991127.991128.991129.991130.991131.991132.991133.991134.991135.991136.991137.991138.991139.991140.991141.991142.991143.991144.991145.991146.991147.991148.991149.991150.991151.991152.991153.991154.991155.991156.991157.991158.991159.991160.991161.991162.991163.991164.991165.991166.991167.991168.991169.991170.991171.991172.991173.991174.991175.991176.991177.991178.991179.991180.991181.991182.991183.991184.991185.991186.991187.991188.991189.991190.991191.991192.991193.991194.991195.991196.991197.991198.991199.991200.991201.991202.991203.991204.991205.991206.991207.991208.991209.991210.991211.991212.991213.991214.991215.991216.991217.991218.991219.991220.991221.991222.991223.991224.991225.991226.991227.991228.991229.991230.991231.991232.991233.991234.991235.991236.991237.991238.991239.991240.991241.991242.991243.991244.991245.991246.991247.991248.991249.991250.991251.991252.991253.991254.991255.991256.991257.991258.991259.991260.991261.991262.991263.991264.991265.991266.991267.991268.991269.991270.991271.991272.991273.991274.991275.991276.991277.991278.991279.991280.991281.991282.991283.991284.991285.991286.991287.991288.991289.991290.991291.991292.991293.991294.991295.991296.991297.991298.991299.991300.991301.991302.991303.991304.991305.991306.991307.991308.991309.99

不四舍五入到下一个数字!有谁知道为什么这些特定数字会发生这种情况!

最佳答案

问题是,当您获取“number + 0.995”的值时,由于二进制浮点不能精确表示十进制值的正常原因,该值不会完全 number + 0.995 .

有时会有点过大,有时会有点过低。当它低于时,并将结果四舍五入到小数点后两位,最终得到“0.99”位。 Math.Round 可能会在某种程度上尝试考虑这一点,但我不确定具体的细节。

在可能的情况下,解决方案是使用decimal而不是double来处理十进制数字重要的事情。

查看我关于 binary floating point 的文章和 decimal .NET 中的浮点以了解更多信息。

这是一个使用我的 DoubleConverter 的程序显示添加 0.995 后不准确的代码:

using System;

class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
{
double d = i + 0.995;
Console.WriteLine(DoubleConverter.ToExactString(d));
}
}
}

结果:

0.99499999999999999555910790149937383830547332763671875 1.99500000000000010658141036401502788066864013671875 2.99500000000000010658141036401502788066864013671875 3.99500000000000010658141036401502788066864013671875 4.99500000000000010658141036401502788066864013671875 5.99500000000000010658141036401502788066864013671875 6.99500000000000010658141036401502788066864013671875 7.99500000000000010658141036401502788066864013671875 8.9949999999999992184029906638897955417633056640625 9.9949999999999992184029906638897955417633056640625 10.9949999999999992184029906638897955417633056640625 11.9949999999999992184029906638897955417633056640625 12.9949999999999992184029906638897955417633056640625 13.9949999999999992184029906638897955417633056640625 14.9949999999999992184029906638897955417633056640625 15.9949999999999992184029906638897955417633056640625 16.995000000000000994759830064140260219573974609375 17.995000000000000994759830064140260219573974609375 18.995000000000000994759830064140260219573974609375 19.995000000000000994759830064140260219573974609375 20.995000000000000994759830064140260219573974609375 ...

(请注意,虽然它在 8 处开始为 0.994,但它一直持续到 15 - 而 Math.Round 在 10 处“修正”自身。)

关于.net - System.Math.Round 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3067526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com