I’m using the math.h library, and when I run the code below I get g++ compile errors telling me “warning: overflow in implicit constant conversion” for multiple lines. However, if I run the executable anyway, it provides me reasonable numbers (although for some reason the Ints and longs maximums are returning the same). But if I use the cmath library, all the signed data types give me negative values and unsigned all return 0….
C++:
/* Calculating data type sizes directly */
/* Shorts - 2 bytes = 2*8(bits/byte) = 16 bits */
smallest_short = -(pow(2, 15));
largest_short = pow(2,15); // LINE 141
us_smallest_short = 0;
us_largest_short = pow(2, 16); // LINE 143
/* Ints - 4 bytes = 4*8(bits/byte) = 32 bits */
smallest_int = -(pow(2, 31));
largest_int = pow(2, 31); // LINE 147
us_smallest_int = 0
us_largest_int = pow(2, 32); // LINE 149
/* Long - 8 bytes = 8*8(bits/byte) = 64 bits */
smallest_long = -(pow(2, 63));
largest_long = pow(2, 63); // LINE 153
us_smallest_long = 0;
us_largest_long = pow(2, 64); // LINE 155
g++ compile errors:
datatypesexp.cpp: In function âint main()â:
datatypesexp.cpp:141: warning: overflow in implicit constant conversion
datatypesexp.cpp:143: warning: overflow in implicit constant conversion
datatypesexp.cpp:147: warning: overflow in implicit constant conversion
datatypesexp.cpp:149: warning: overflow in implicit constant conversion
datatypesexp.cpp:153: warning: overflow in implicit constant conversion
datatypesexp.cpp:155: warning: overflow in implicit constant conversion
Should I stick with math.h? How do I resolve the warnings?
Also,
if I run the exec with math.h, disregarding the warnings, my signed “largest int” and largest long” both return 2147483647.
While unsigned-wise they both return 4294967295. But longs should be returning a greater value…
How could I remedy this?
The positive values overflow because on that side of the scale there is a zero to accomodate, leaving you with one value less. For example,
pow(2, 31)is 2,147,483,648 (represented before assignment as double), but the largest signed integer in this case is 2,147,483,647.