问题标签 [numeric-limits]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
169 浏览

c++ - Why does the standard provide both is_integer and is_exact?

std::numeric_limits provides 2 constants that are mutually exclusive:

  • is_integer : "true for all integer arithmetic types T"

  • is_exact: "true for all arithmetic types T that use exact representation"

Is there the possibility of a non-exact integral type? What is trying to be allowed for here?

In all my templates where I to know if I am dealing with precise numbers, I used is_integer, do I need to go add a check for is_exact as well now?

0 投票
2 回答
172 浏览

c - (~INT_MAX && INT_MAX) 返回 1?

我的印象是 INT_MAX 将打开一个 int 的所有 32 位。如果我否定它并将它与自身“和”它,我应该将所有 0 与所有 1 进行比较并返回错误。我错过了什么?

编辑:哦,哇,我也在翻转符号位。使用 UNIT_MAX 给了我我需要的结果。谢谢大家!

0 投票
1 回答
227 浏览

c++ - INT_MAX 除以自身的两倍 (INT_MAX * 2)

在 C++ 中,我想知道如何获得“INT_MAX / (INT_MAX + INT_MAX)”的正确答案 0.5?我尝试将除数/除数和除数都转换为长,并将除数转换为双倍,全部返回-1073741823。谁能给我一些建议?谢谢。

0 投票
1 回答
1667 浏览

c++ - 如何在 C++ 中仅输入整数而不破坏剩余代码?

我希望我的代码只输入整数。下面的代码正确地完成了它的工作,并在未使用整数时询问用户输入。但是,添加代码后:

进入下面的代码,它仅在我先输入非整数时才有效。否则,如果我先输入一个 int,则程序不会继续执行下一条语句,也不会执行任何操作。我的理由是,如果 x = int 则不会启动 while 循环。那么为什么添加代码会弄乱剩余的代码。

0 投票
1 回答
994 浏览

c++ - UINT_MAX 宏的定义

我想知道是否有特殊原因将宏定义UINT_MAX为头文件中的真实值,(2147483647 * 2U + 1U) 而不是直接定义它的真实值。(4294967295U)climits

谢谢你们。

0 投票
2 回答
362 浏览

c++ - 是否有浮点指数偏差的规范?

IEEE 浮点指数存储为无符号整数,使用预定义的指数偏差来抵消指数。

指数偏差似乎始终等于浮点类型的位置numeric_limits<T>::max_exponent - 1T

但是,我没有任何文档说明这始终是正确的,而且我当然对非 IEEE 浮点格式一无所知。

这对于以下功能必须是已知的:

是否有它的规范,还是我必须假设numeric_limits<T>::max_exponent - 1

0 投票
1 回答
540 浏览

c++ - Is there a reason why numeric_limits do not work on reference types?

If you mistakenly do something like:

You will get unhelpful error message from the file in the STL implementation.

Problem is that template argument is a reference, so the fix is to remove it:

Now my question is why numeric_limits do not know to do this by themselves? I would understand that you do not want to remove pointerness(since max of char pointer and max of char are very very different things), but I would assume that whenever you have a reference as an argument to numeric_limits you would be happy with result that is obtained by removing it.

0 投票
2 回答
2142 浏览

c++ - numeric_limits 的值::max() 在 C++ 中

在 c++ 中使用cin.ignore()时,它需要一个参数,即要消耗的字符数,直到出现分隔符。大多数情况下,我观​​察到要使用以下内容cin.ignore(numeric_limits<streamsize>::max(), '\n');

我很想知道 的值,numeric_limtis<streamsize>::max()所以我只输出了它的值,它变成了一个巨大的值 9223372036854775807 。如果它代表字符的数量,那么它可以以字节为单位来考虑,如果是这样,这不是一个非常大的值,超出了我的硬盘空间。

有人可以告诉我它实际上是什么以及为什么这么大的价值吗?

0 投票
2 回答
120 浏览

c++ - max_digits10 为 0 对浮点类型的引用有什么好处?

在 C++11 中,为浮点数的引用std::numeric_limits<Type>::max_digits10返回 0 有什么好处?Type

例如:

precisionPositive返回 9 并precisionZero返回 0 。

在什么情况下零值会有所帮助,而不是给出编译时错误?

0 投票
1 回答
908 浏览

c++ - __FLT_MAX__ 和 __DBL_MAX__ 为 0?

在 GCC 9.1 中,当使用浮点类型调用 std::numeric_limits 的函数时,它们在大多数情况下返回 0。

这发生在我正在处理的项目中,MSVC、GCC 8.3 或 Clang 8.0 没有问题。<double>::epsilon()有时具有正确的值,但是当从其他文件调用时,它也可能评估为 0。

<int>::max()此处留作参考)

产生一个独立的文件(正确的值):

项目结果:

独立编译专用文件时,这些值是正确的:问题不是来自 GCC(正如我所料),但很可能来自项目的配置。

编辑:独立编译项目文件(目前出现此问题)也可以提供正确的结果。随着gcc -dM -E__DBL_MAX__被定义为double(1.79769313486231570814527423731704357e+308L)

__DBL_MAX__值被定义,被 ifdef 包围的代码被执行:

GDB 给出了相同的精确值,所以输出部分没有问题。输出XXX_YYY__XXX_YYY__给出相同的结果,因为 numeric_limits 的函数无论如何都会调用它们。

100%明确:std::numeric_limits<double>::max() == 0返回true,所以输出部分没有问题。它只是留在这里作为参考。

GCC 产生这种行为的原因可能是什么?反正不是__XXX_YYY__内置值吗?他们怎么可能持有0?