8 Replies - 1377 Views - Last Post: 21 August 2020 - 12:01 PM Rate Topic: -----

#1 random Goose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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. :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Float and Unsigned Int maximum values.

#2 random Goose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15894
  • View blog
  • 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:

Link

While an int is just what it is.
Was This Post Helpful? 1
  • +
  • -

#4 random Goose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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. :dontgetit: )
But why sizeof doesn't return the "real" size!?
Was This Post Helpful? 0
  • +
  • -

#5 random Goose   User is offline

  • D.I.C Head

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

Re: Float and Unsigned Int maximum values.

Posted 20 August 2020 - 02:16 PM

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

Thank you! (I thought I was going crazy. I need to get some sleep. :dontgetit:/> )
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 ?! :blink:
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15894
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: Float and Unsigned Int maximum values.

Posted 20 August 2020 - 02:56 PM

View Postrandom 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 ?! :blink:/>

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.
Was This Post Helpful? 1
  • +
  • -

#8 random Goose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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! :nottalkingtoyou:
*Pardon my English, I'm from old Europe. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7530
  • View blog
  • 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.

If you wanted to learn more about floating point numbers, there is this classic paper:
What Every Computer Scientist Should Know About Floating-Point Arithmetic
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1