Float and Unsigned Int maximum values.

Page 1 of 1

8 Replies - 1377 Views - Last Post: 21 August 2020 - 12:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=419943&amp;s=8f9ea7d608a4c5794528f03b3e29854c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 random Goose

Reputation: 1
• Posts: 50
• Joined: 07-August 20

Float and Unsigned Int maximum values.

Posted 20 August 2020 - 12:50 PM

I'm not quite understand why float and unsigned int both fit in 4 bytes, but float has larger min\max values than unsigned int despite that unsigned int has one additional (sign)bit to code a numerical value! Exact opposite is expected (value of u.Int must be larger than maximum (positive OR negative, - absolute) value of float). What magic is that!? (checked on 64bit machine, with GCC.) Maybe I didn't get something here.

Is This A Good Question/Topic? 0

Replies To: Float and Unsigned Int maximum values.

#2 random Goose

Reputation: 1
• Posts: 50
• Joined: 07-August 20

Re: Float and Unsigned Int maximum values.

Posted 20 August 2020 - 01:16 PM

```float fv = 1.0;
unsigned int iv = 0; iv--;
cout << "\n" << "Size of float: "<< sizeof(fv) << "\n";
cout << setprecision(42) << "Max val of Float: " << numeric_limits<float>::lowest() << "\n";
cout << "Size of unsigned Int: " << sizeof(iv) << "\n" << "Max val of Unsigned Int: " << iv <<"\n";
```

Output:

Size of float: 4
Max val of Float: -340282346638528859811704183484516925440
Size of unsigned Int: 4
Max val of Unsigned Int: 4294967295

#3 modi123_1

• Suitor #2

Reputation: 15894
• Posts: 63,609
• Joined: 12-June 08

Re: Float and Unsigned Int maximum values.

Posted 20 August 2020 - 01:31 PM

Remember that floats are typically held as a sign + exponent + mantissa.

This has a good break down of that:

While an int is just what it is.

#4 random Goose

Reputation: 1
• Posts: 50
• Joined: 07-August 20

Re: Float and Unsigned Int maximum values.

Posted 20 August 2020 - 01:54 PM

Thank you! (I thought I was going crazy. I need to get some sleep. )
But why sizeof doesn't return the "real" size!?

#5 random Goose

Reputation: 1
• Posts: 50
• Joined: 07-August 20

Re: Float and Unsigned Int maximum values.

Posted 20 August 2020 - 02:16 PM

random Goose, on 20 August 2020 - 01:54 PM, said:

Thank you! (I thought I was going crazy. I need to get some sleep. /> )
But why sizeof doesn't return the "real" size!?

*I meant "size of result". Now i get it.
But isn't that terribly ineffective? So it's really running the computation every time some floating point value is fetched ?!

#6 modi123_1

• Suitor #2

Reputation: 15894
• Posts: 63,609
• Joined: 12-June 08

Re: Float and Unsigned Int maximum values.

Posted 20 August 2020 - 02:46 PM

Operates similar to how characters get represented as bytes to display, or a binary value gets turned into an integer.

#7 Skydiver

• Code herder

Reputation: 7530
• Posts: 25,321
• Joined: 05-May 12

Re: Float and Unsigned Int maximum values.

Posted 20 August 2020 - 02:56 PM

random Goose, on 20 August 2020 - 05:16 PM, said:

But isn't that terribly ineffective? So it's really running the computation every time some floating point value is fetched ?! />

sizeof() is evaluated at compile time, not at runtime. And this is very cheap for the compiler since the compiler knows what the data type of every symbol it encounters, the data type determines the size of the that object.

#8 random Goose

Reputation: 1
• Posts: 50
• Joined: 07-August 20

Re: Float and Unsigned Int maximum values.

Posted 21 August 2020 - 05:07 AM

Absolutely fascinating things. Nobody explained that kind of things in class! All I remember is: "...because the way doubles and floats are represented, they may have weird behavior. But it`s not important in real world programming." What a scam. I must learn now this kind of things about architecture on my own!
*Pardon my English, I'm from old Europe.

#9 Skydiver

• Code herder

Reputation: 7530
• Posts: 25,321
• Joined: 05-May 12

Re: Float and Unsigned Int maximum values.

Posted 21 August 2020 - 12:01 PM

It's like learning how to drive a car. The driving instructor won't require you to learn about the chemistry and physics of what makes a car work. They are more concerned about you picking up the skills to use the car. It's only on your interest level if you want to learn more.

I recall my scuba diving instructor who is really into the physical and chemical effects of diving but he would only talk about these things during downtime while on our boat rides or rest periods. During class time though, he only covers the bare necessities of how nitrogen gets absorbed and released by the body and why keeping track of depths and dive times is important so that we don't get the bends. He doesn't go into the nitty gritty details like he does during the breaks.