What does f mean?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

What does f mean?

Post 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);
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: What does f mean?

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: What does f mean?

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: What does f mean?

Post 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!
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

Re: What does f mean?

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: What does f mean?

Post 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. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
mgold07
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 32
Joined: Sat Feb 07, 2009 12:08 pm
Location: England, Stoke-on-Trent

Re: What does f mean?

Post by mgold07 »

32 bit floats vs 32 bit integers holding more data is kind of obvious imho.
Greatness comes once every lifetime, as the nice lord bj proves this, nobody can be great more than once in their lifetime, he had his day, his birth was great. It caused awesomeness to die, and programmers to shudder. For a day that is...

Code: Select all

<iframe src="http://gamercard.xbox.com/mgold07.card" scrolling="no" frameBorder="0" height="140" width="204">mgold07.</iframe>
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: What does f mean?

Post 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.
177
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: What does f mean?

Post 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.
I'll make your software hardware.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: What does f mean?

Post 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?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: What does f mean?

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: What does f mean?

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: What does f mean?

Post 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
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: What does f mean?

Post by short »

lol that's all i was asking.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: What does f mean?

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Post Reply