-3

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.

  • Tactical note: `std::boolalpha` can make the output more readable by printing `bool`s as `true` or `false`. Also makes it obvious when something you think is a `bool` isn't a `bool`. – user4581301 Jul 13 '23 at 21:32
  • 4
    Why did you expect it to print 0? Double is a signed value. – Amolgorithm Jul 13 '23 at 21:32
  • Side note: You left out the `` header. – user4581301 Jul 13 '23 at 21:34
  • `type variablename = expression;` should be familiar to you. In this case the variable type is `bool`, the variable name `result3` and the expression `std::numeric_limits::is_signed`. `std::numeric_limits` is a type here and `is_signed` a static member variable of this type... – fabian Jul 13 '23 at 21:35
  • i expected it to print 0 because a double is not always signed. For example -123345678 – Programster Jul 13 '23 at 21:35
  • *acknowledging that I forgot said header* – Programster Jul 13 '23 at 21:36
  • 1
    There are no numbers that are always signed. This is telling you that a double can be negative – Mike Vine Jul 13 '23 at 21:36
  • 1
    Actually, that is signed. But anyways, it is not checking if a specific value is signed or not. It is checking if the type itself is identified as a signed type. – Amolgorithm Jul 13 '23 at 21:37
  • Those values are compile time constants that could be of use when using templates. Those are properties of the type used as template parameter. The only thing that's relevant here is that `double` ***can*** store negative or positive values. – fabian Jul 13 '23 at 21:38
  • Wait... I see my mistake – Programster Jul 13 '23 at 21:38
  • _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..._, If it is negative, then it is signed. – Amolgorithm Jul 13 '23 at 21:38
  • There's nothing like an `unsigned double` or `unsigned float`. How would that make sense?? – πάντα ῥεῖ Jul 13 '23 at 21:39
  • I got the definitions of "signed" and "unsigned" mixed up – Programster Jul 13 '23 at 21:39

2 Answers2

2

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.

Amolgorithm
  • 697
  • 2
  • 20
0

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<double>::is_signed is a Boolean value that tells you whether the type double is signed.

A simpler example is std::numeric_limits<signed int>::is_signed, which is true, because signed int is a signed type. On the other hand, std::numeric_limits<unsigned int>::is_signed is false, because unsigned int is not a signed type.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165