相关疑难解决方法(0)

浮点数学是否破碎?

请考虑以下代码:

0.1 + 0.2 == 0.3  ->  false
Run Code Online (Sandbox Code Playgroud)
0.1 + 0.2         ->  0.30000000000000004
Run Code Online (Sandbox Code Playgroud)

为什么会出现这些不准确之处?

language-agnostic math floating-point floating-accuracy

2798
推荐指数
28
解决办法
28万
查看次数

Printf宽度说明符,用于保持浮点值的精度

是否有printf宽度说明符可以应用于浮点说明符,该说明符会自动将输出格式化为必要的有效位数,以便在重新扫描字符串时,获取原始浮点值?

例如,假设我打印float2小数位数的精度:

float foobar = 0.9375;
printf("%.2f", foobar);    // prints out 0.94
Run Code Online (Sandbox Code Playgroud)

当我扫描输出时0.94,我没有符合标准的保证我将获得原始的0.9375浮点值(在这个例子中,我可能不会).

我想要一种方法告诉printf自动将浮点值打印到必要的有效位数,以确保它可以扫描回传递给的原始值printf.

我可以使用一些宏float.h导出要传递的最大宽度printf,但是是否已经有一个说明符可以自动打印到必要的有效位数 - 或者至少是最大宽度?

c floating-point printf c99 floating-point-precision

84
推荐指数
8
解决办法
21万
查看次数

float 和 double 的实际最小/最大值是多少(C++)

我已经阅读了对浮动使用“FLT_MIN”和“FLT_MAX”值的建议。每当我这样做时,代码块都会告诉我

最大值:3.40282e+038 最小值:1.17549e-038

不知道这意味着什么我试图获得真正的价值并得到了

最大值:47.2498237715 最小值:-34.8045265148

......但这些并不能说明问题。

这是我的代码片段

   char c;         // reserve: 1 byte, store 1 character (-128 to 127)
   int i;          // reserve: 4 bytes, store -2147483648 to 2147483657
   short int s;    // reserve: 2 bytes, store -32768 to 32767
   float f;        // reserve: 4 bytes, store ?? - ?? (? digits)
   double d;       // reserve: 8 bytes, store ?? - ?? (? digits)
   unsigned int u; //reserve: r bytes store 0 to 4294967295

   c = 'c';
   cout << …
Run Code Online (Sandbox Code Playgroud)

c++ variables floating-point double

8
推荐指数
3
解决办法
4万
查看次数

如何计算浮点型精度,是否有意义?

我在理解浮点类型的精度时遇到问题。msdn将该精度写入6 到 9 位数字。但我注意到精度取决于数字的大小:

  float smallNumber = 1.0000001f;
  Console.WriteLine(smallNumber); // 1.0000001

  bigNumber = 100000001f;
  Console.WriteLine(bigNumber); // 100000000

Run Code Online (Sandbox Code Playgroud)

smallNumber 比 big 更精确,我了解 IEEE754,但我不明白 MSDN 如何计算精度,这是否有意义?

此外,您可以在此处使用浮点格式的数字表示。请在“您输入”输入中输入 100000000 值,然后单击右侧的“+1”。然后将输入的值更改为 1,并再次单击“+1”。您可能会看到精度上的差异。

c# math floating-point ieee-754

0
推荐指数
1
解决办法
291
查看次数