We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
0.0f (Read 740 times)
0.0f
Jan 23rd, 2007, 4:40pm
 
sorry if this is a completely retarded question, but in several threads here i've seen people set a float value to Code:

0.0f


as opposed to just 0.0 or simply 0

what is the 'f' for, and why is that a better method than the other two i listed?

sorry if thats lame, but its been bothering me.

thanks guys!
Re: 0.0f
Reply #1 - Jan 23rd, 2007, 4:51pm
 
f is for float as is any number including a dot "." ending with 'f' :

5f
0.5f

( see next post about doubles )

but

5 will be an int type

F ( as in Florian )
Re: 0.0f
Reply #2 - Jan 23rd, 2007, 5:09pm
 
I think it's more of a hold-over from java where any number with a . in it is assumed to be a double, unless you follow it with f where it becomes a float.
Re: 0.0f
Reply #3 - Jan 23rd, 2007, 5:19pm
 
uups. right. here's the full list of java types:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

F
Re: 0.0f
Reply #4 - Jan 23rd, 2007, 11:26pm
 
in java, the number 0.1 is a double, and 0.1f is a float. doubles are 64 bits, floats are 32 bits. floats are sufficient for nearly all graphics for which processing was intended, so doubles are avoided for the api in order to make things faster and make the ram footprint smaller.

the p5 preprocessor converts 0.1 to 0.1f behind the scenes, because peppering one's code with 'f's is annoying for advanced users, and confusing for learners.

edit: more info...

so to be clear in processing, all of this code is identical:

float a = 1;
float b = 1.0;
float c = 1.0f;

there's no benefit to one over the other. for instance 0 is converted to 0.0f by the java compiler, so it's not as though the type conversion is happening in your code.

proof for the geeky, the javap dump of this code:
Code:

public class floater {
static public void main(String[] args) {
float a = 1;
float b = 1.0f;
}
}

looks like the following. this is raw java bytecode, fconst and fstore refer to loading a floating point number that's a constant (1.0) and then storing it as a local variable to be used.
Code:

: javap -c floater
Compiled from "floater.java"
public class floater extends java.lang.Object{
public floater();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: fconst_1
1: fstore_1
2: fconst_1
3: fstore_2
4: return
}


another further annoyance is that between java a c/c++, the syntax is different. for instance, 0.1f is ok for c/c++ but while 1f is ok for java, it's not for c/c++ (at least on the compilers i was using at the time that we were first developing processing).
Page Index Toggle Pages: 1