Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数学。地板行为_C#_.net - Fatal编程技术网

C# 数学。地板行为

C# 数学。地板行为,c#,.net,C#,.net,上述代码在一种配置下输出“3 2”,其中所有数字均为双倍。这怎么可能?是因为双重运算导致的分数误差吗?然而,我认为a.ToString()应该给出带小数部分的整数。这只是一个什么是整数的问题。下面是一个简短但完整的程序,演示了同样的事情: double c, d, e; double a = (c - d) / e; double b = Math.Floor(a); Debug.WriteLine(a.ToString() + " " + b.ToString()); 输出: using S

上述代码在一种配置下输出“3 2”,其中所有数字均为双倍。这怎么可能?是因为双重运算导致的分数误差吗?然而,我认为
a.ToString()
应该给出带小数部分的整数。

这只是一个什么是整数的问题。下面是一个简短但完整的程序,演示了同样的事情:

double c, d, e;
double a = (c - d) / e;
double b = Math.Floor(a);
Debug.WriteLine(a.ToString() + " " + b.ToString());
输出:

using System;

public class Test
{
    static void Main(string[] args)
    {
        // Find the largest double less than 3
        long bits = BitConverter.DoubleToInt64Bits(3);
        double a = BitConverter.Int64BitsToDouble(bits - 1);
        double b = Math.Floor(a);
        // Print them using the default conversion to string...
        Console.WriteLine(a.ToString() + " " + b.ToString());
        // Now use round-trip formatting...
        Console.WriteLine(a.ToString("r") + " " + b.ToString("r"));
    }
}
现在,
double.ToString()
的文档包括:

此版本的ToString方法隐式使用通用数字格式说明符(“G”)和当前区域性的NumberFormatInfo

。。。文件规定:

精度说明符定义结果字符串中可以出现的最大有效位数。如果省略了精度说明符或为零,则数字的类型将确定默认精度,如下表所示

。。。其中,表格显示
double
的默认精度为15。如果你考虑2.9999999999999996个舍入到15个有效数字,最终会有3个。

实际上,
a
的准确值如下:

3 2
2.9999999999999996 2

。。。当用15位有效数字来表示时,它也是3。

这是一个不完整的代码片段。您没有显示
c
d
e
的定义或值。我猜
a
类似于
2.999999999968768
义务参考:@AlpHancioglu,正如我所说,您应该显示
c
d
e
的定义和值。
2.999999999999999555910790149937383830547332763671875