Page 1 of 2

What does f mean?

Posted: Thu May 27, 2010 8:14 am
by Bullet Pulse
I just started learning OpenGL and I want to know what f means in the example below.

Code: Select all

glVertex3f(1.0f, 0.0f, 0.0f);

Re: What does f mean?

Posted: Thu May 27, 2010 8:15 am
by Falco Girgis
Bullet Pulse wrote:I just started learning OpenGL and I want to know what f means in the example below.

Code: Select all

glVertex3f(1.0f, 0.0f, 0.0f);
You are explicitly telling the compiler to treat the numerical constants as floats.

Re: What does f mean?

Posted: Thu May 27, 2010 8:51 am
by avansc
try this out.

printf("%d\n", sizeof(1.0));
printf("%d\n", sizeof(1.0f));

by default C will cast any decimal number to a double, the f tells it to cast to float just like falco said.
and the f that is appended on the gl call indicates it takes floats.

Re: What does f mean?

Posted: Thu May 27, 2010 10:09 am
by Falco Girgis
avansc wrote:try this out.

printf("%d\n", sizeof(1.0));
printf("%d\n", sizeof(1.0f));

by default C will cast any decimal number to a double, the f tells it to cast to float just like falco said.
and the f that is appended on the gl call indicates it takes floats.
OoOo, great way to illustrate that!

Re: What does f mean?

Posted: Thu May 27, 2010 5:39 pm
by Randi
you may have to change %d to %ld, my compiler was complaining about that. It seems weird to me that a float takes up the same amount of space as a regular int.

Re: What does f mean?

Posted: Thu May 27, 2010 7:39 pm
by Ginto8
Randi wrote:It seems weird to me that a float takes up the same amount of space as a regular int.
Well that's because the floating point coprocessor (IIRC) has the same word size as a the normal processor. Also, 32 bit floats hold a lot more information than 32 bit integers. Look up how floating point works if you're interested. ;)

Re: What does f mean?

Posted: Fri May 28, 2010 6:00 am
by mgold07
32 bit floats vs 32 bit integers holding more data is kind of obvious imho.

Re: What does f mean?

Posted: Fri May 28, 2010 12:28 pm
by Amarant
32 bit floats don't hold a lot more information than 32 bit integers.
They also don't hold more data than 32 bit integers.

What they can do is represent much larger and much smaller values. You're still limited to the 2^32 different combinations you can make using 32 bits. The only thing that's different is the interpretation of these 32 bits.

For example:

Code: Select all

FLT_MAX=340282346638528859811704183484516925440.0000......
FLT_MIN=0.0000000000000000000000000000000000000117549435082228750796873653722224567781866555677208752150875170627841725945472717285156250000.....
In doing so they give up precision though.
If you were to subtract FLT_MIN from FLT_MAX you will notice the result will still equal FLT_MAX.
In other words, you cannot represent every value between FLT_MAX and FLT_MIN using a float.

Re: What does f mean?

Posted: Fri May 28, 2010 1:49 pm
by thejahooli
Don't floating points act like standard form which is the reason they can hold large numbers but can't be precise.
I don't actually know this as a fact but it seems like a reasonable idea.

Re: What does f mean?

Posted: Fri May 28, 2010 1:57 pm
by short
thejahooli wrote:Don't floating points act like standard form which is the reason they can hold large numbers but can't be precise.
I don't actually know this as a fact but it seems like a reasonable idea.
what do you mean standard form?

Re: What does f mean?

Posted: Fri May 28, 2010 2:04 pm
by Ginto8
short wrote:
thejahooli wrote:Don't floating points act like standard form which is the reason they can hold large numbers but can't be precise.
I don't actually know this as a fact but it seems like a reasonable idea.
what do you mean standard form?
floating point is basically a form of scientific notation. The "mantissa" (IIRC, correct me if I'm wrong) contains a fixed-point fractional number. The "exponent" part is x in a * 10^x, where a is the mantissa. There's other stuff too, like information about sign, but that's the general jist of floating point.

Re: What does f mean?

Posted: Sat May 29, 2010 10:46 pm
by short
Ginto8 wrote:
short wrote:
thejahooli wrote:Don't floating points act like standard form which is the reason they can hold large numbers but can't be precise.
I don't actually know this as a fact but it seems like a reasonable idea.
what do you mean standard form?
floating point is basically a form of scientific notation. The "mantissa" (IIRC, correct me if I'm wrong) contains a fixed-point fractional number. The "exponent" part is x in a * 10^x, where a is the mantissa. There's other stuff too, like information about sign, but that's the general jist of floating point.
by standard form does he mean scientific notation? That's what I was asking.

Re: What does f mean?

Posted: Sun May 30, 2010 12:44 pm
by Ginto8
short wrote:by standard form does he mean scientific notation? That's what I was asking.
I believe so. At least that's what this says: http://www.mathsrevision.net/gcse/pages.php?page=43

Re: What does f mean?

Posted: Sun May 30, 2010 3:10 pm
by short
lol that's all i was asking.

Re: What does f mean?

Posted: Sun May 30, 2010 7:22 pm
by RyanPridgeon
Ginto8 wrote:
short wrote:
thejahooli wrote:Don't floating points act like standard form which is the reason they can hold large numbers but can't be precise.
I don't actually know this as a fact but it seems like a reasonable idea.
what do you mean standard form?
floating point is basically a form of scientific notation. The "mantissa" (IIRC, correct me if I'm wrong) contains a fixed-point fractional number. The "exponent" part is x in a * 10^x, where a is the mantissa. There's other stuff too, like information about sign, but that's the general jist of floating point.
Almost, but the exponent is 2 ^ x, not 10 ^ x. Basically the first block of bits (called the mantissa) is a fixed point number x which is -1 <= x < 1, and the next block (the exponent) is a (usually signed) integer which determines the power of 2 to multiply by.

For example if our mantissa is 01101, the first bit represents -1, the next 0.5, the next 0.25 etc halving each time. So we can write this as 0.1101

Then let's say our exponent is 010, which is 2 in binary. Then we move the point on our mantissa 2 places to the right (x * 2^y) to get the final value of 11.01, which is 2 + 1 + 0.25 = 3.25.

Dunno how well I explained that, but basically its a set of digits that can be shifted around to make large or small values without needing total accuracy.

It's also useful to note that some values (such as 0.1) cannot be stored in binary. This is similar to how 1/3 cannot be displayed in decimal, it simply doesn't work. You can half 1, 0.5, 0.25 as long as you like and theyll never add up to 0.1. It is this and the problem of losing accuracy that lead to floating point troubles when comparing 2 floating point numbers. This is why you should never do something like " if (float1 == float2) { " because sometimes they won't be equal when you expect them to be.