Search code examples
c++booleanconstant-expressionnumeric-limits

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


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't understand is the code to the right of the =

The books explanation is "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."

  • Are we trying to find out if the value stored in "result3" is signed? If so, how does that
    contend with the fact that we haven't even initialized "result3" 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'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.


Solution

  • 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 "in general".

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

    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.
    So here is a good thorough definition on signedness.