(C++) 这个表达式究竟是做什么的?numeric_limits<double>::is_signed

huangapple go评论64阅读模式
英文:

(C++) What exactly does this expression do? numeric_limits<double>::is_signed

问题

我在一本C++教材中找到了以下内容:

```cpp
bool result3 = numeric_limits<double>::is_signed;         //result3保存了一个true值
bool result4 = numeric_limits<short>::is_integer;         //result4保存了一个true值
bool result5 = numeric_limits<float>::is_exact;           //result5保存了一个false值

我理解上述语句声明并初始化了一个保存布尔值的变量。我不理解的部分是等号右边的代码。

该书的解释是:“numeric_limits类还包括上述语句集合中显示的静态常量。这些常量提供了一种检查值是否为有符号、是否为整数类型或是否为精确类型的方法。”

  • 我们是想找出存储在“result3”中的值是否为有符号吗?如果是的话,这如何与我们尚未初始化“result3”这一事实相符呢?

  • 我们是想找出double值是否一般都是有符号的吗?我知道事实上double值可以为负数,因此返回true值似乎是没有意义的。

我尝试在Xcode上运行以下代码:

#include <iostream>
using namespace std;
int main()
{
    bool result3 = numeric_limits<double>::is_signed;   
    cout << result3;
    return 0;
}

我预期它会在屏幕上打印0而不是1,但它打印了1。


<details>
<summary>英文:</summary>

I came accross the following in a C++ textbook:

bool result3 = numeric_limits<double>::is_signed; //result3 holds a value of true
bool result4 = numeric_limits<short>::is_integer; //result4 holds a value of true
bool result5 = numeric_limits<float>::is_exact; //result5 holds a value of false


I understand that the above statements declare and initialize a variable that holds a boolean value. The part I don&#39;t understand is the code to the right of the =

The books explanation is **&quot;The numeric_limits class also includes the static consonants shown in this figure [figure is of the above set of statements]. These consonants provide a way to check whether a value is signed, is an integer type, or is an exact type.&quot;**

- Are we trying to find out if the value stored in &quot;result3&quot; is signed? If so, how does that  
  contend with the fact that we haven&#39;t even initialized &quot;result3&quot; yet?

- Are we trying to find out if double values in general are signed? I know for a fact that a double
  value can be negative, so it wouldn&#39;t make sense for that to return a value of true.




I tried running the following on Xcode:

#include <iostream>
using namespace std;
int main()
{
bool result3 = numeric_limits<double>::is_signed;
cout << result3;
return 0;
}


I expected it to print a 0 to the screen instead of a 1, but it printed a 1.


</details>


# 答案1
**得分**: 2

我们正在查找双精度浮点数是否是有符号的。没有“一般性”之分。

双精度浮点数是*有符号的*。没有“无符号双精度”这种东西。

我不确定你为什么期望它打印0而不是1,但它应该打印1。程序是正确的。

这是因为`double`值是有符号的,没有“无符号双精度”这个概念。

我认为你可能把“有符号”和“无符号”搞混了。
<br>
所以这里有一个关于[有符号性](https://en.wikipedia.org/wiki/Signedness)的详细定义。

<details>
<summary>英文:</summary>

&gt; Are we trying to find out if double values in general are signed?

We are finding out if double values are signed. There is no &quot;in general&quot;.

Double values are *signed*. There is no such thing as an `unsigned double`.

&gt; I expected it to print a 0 to the screen instead of a 1, but it printed a 1.

I am not sure why you expected it to print a 0, but it was supposed to print a 1. The program is correct.

This is because `double` values are signed, as there is no such thing as an `unsigned double`.

I think you may have mixed up `signed` and `unsigned`.
&lt;br&gt;
So here is a good thorough definition on [signedness](https://en.wikipedia.org/wiki/Signedness).


</details>



# 答案2
**得分**: 0

`numeric_limits` 模板(不是类)可用于实例化数值类型;生成的实例化会告诉您有关数值类型的属性。

所以 `std::numeric_limits<double>::is_signed` 是一个布尔值,告诉您类型 `double` 是否是有符号的。

一个更简单的示例是 `std::numeric_limits<signed int>::is_signed`,它为 `true`,因为 `signed int` 是有符号类型。另一方面,`std::numeric_limits<unsigned int>::is_signed` 为 `false`,因为 `unsigned int` 不是有符号类型。

<details>
<summary>英文:</summary>

The `numeric_limits` **template** (not class) can be instantiated for numeric types; the resulting instantiation tells you about the properties of the numeric type.

So `std::numeric_limits&lt;double&gt;::is_signed` is a Boolean value that tells you whether the type `double` is signed.

A simpler example is `std::numeric_limits&lt;signed int&gt;::is_signed`, which is `true`, because `signed int` is a signed type. On the other hand, `std::numeric_limits&lt;unsigned int&gt;::is_signed` is `false`, because `unsigned int` is not a signed type.

</details>



huangapple
  • 本文由 发表于 2023年7月14日 05:29:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定