max values for Uint64 and float?

camelCase

The Case of the Mysterious Camel.
Reaction score
362
I was bored and decided to look through limits.h
I saw a few weird things like how ULLONG_MAX is supposed to be 2^64 but ended up becoming (2^64)-1 in the "Locals" window when debugging.

And how converting the max value of a float to a Uint64 is (2^63)

Code:
    unsigned long long ull = ULLONG_MAX; //18446744073709551615 = (2^64)-1
    float f = FLT_MAX; //???

    float ull_f = static_cast<float>(ull); //Not enough precision to represent correctly
    unsigned long long f_ull = static_cast<unsigned long long>(f); //2^63

Guess I was messing around because I never understood the max value of a float compared to that of an Uint64.
I was trying to figure out which had the higher value =x

I'm guessing float > Uint64.
What's really weird, to me, is that the max value of a Uint64 is (2^64)-1.
 

LocalDude

New Member
Reaction score
4
If i understand you correctly you are wondering about the (2^64)-1

the 2 stands for the base of the binary
64 is the number of bits
-1 is because the first value is 0

so a float (2^32)-1 is less then (2^64)-1
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
For the (2^64)-1 part, I kinda' remembered why after I got off the PC, thanks.

I never understood this whole floating-point business.
Anyway, trying to cast an unsigned long long to a float gives me this value:
1.8446744e+019

Isn't it the same as: 1.8446744 x 10^19 ?
It is roughly the max value of an unsigned long long but with some values missing because it doesn't have enough precision.

Also, a float's max value is: 3.4028235e+038
Isn't is the same as: 3.4028235 x 10^38 ?

Which is more than the resulting value of casting an unsigned long long to a float.
So, doesn't this mean that a float > unsigned long long ?
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
It doesn't exactly depend on the "size" of the number but on the number of significant digits. That means the number of digits when you cut away the leading and trailing zeroes.

That's because the float consists of two parts. The exponent (e) and the significand (s).
The exponent is of 8 bits size, so −126 to 127 while the significand covers the rest.

A float is constructed so that s * 10^e is as close to the number you've entered as possible.

So it's a very different design compared to the usual twos-complement integral data types.

When you represent numbers with a lot of significants like 1.234567890 you split the two elements:
significand: .123456789
exponent: 1 ( because .123456789 * 10^(1) = 1.234567890 )

So there's no problem storing the exponent, but the significand is a problem. You can't 100% accurately store it in those few bits.
That's where the inaccuracy comes from.

That's why you might be able to store 200000000 without problems but 199999999 leads to a loss of precision.

Uhh that's how I remember it at least. I think at least most of it is sound. But don't take my word for it unconditionally. Might be that I got something wrong.

PS: So float might have a larger range than int, but int can represent every value correctly, while float has lotsa lotsa tiny holes that it can't represent. Actually float has a state for infinity and -infinity, so yea. float's min/max range is (-infinity, infinity) xD
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Ahh..
Thanks, guys.

One of my lecturers was recently ranting on and on about defensive programming and said that for our assignment, he wouldn't care about efficiency but if we failed to have necessary error-checks in our code, we'd be shot dead.

I figured one of the bigger potential gotcha's was conversion from one arithmetic type to another.
Needless to say, I had to wrap all conversions in wrapper functions with checks.

Thanks for clearing my doubts.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Ahh..
Thanks, guys.

One of my lecturers was recently ranting on and on about defensive programming and said that for our assignment, he wouldn't care about efficiency but if we failed to have necessary error-checks in our code, we'd be shot dead.

Some also call it Paranoid Programing :p
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Hahahahahaha.
I just find it a little sad =/

The nice warning messages sprinkled all over my code doesn't show up; they never get their chance at showing.
They must be lonely =(
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top