0

It seems to be used in value declarations in the engine but the documentation of JBox-2D does not explain what this 'unit' is. Example:

float x = 20.0f //<-- this 'f'

FixtureDef fixDef = new FixtureDef();
fixDef.shape = shape;
fixDef.density = 0.0f; //<-- this 'f'
fixDef.friction = 0.9f; //<-- this 'f'

What is it? If it is indeed a unit, what is it relative to? What benefit does it have for the engine?

EDIT: What use does it have for the engine? Is there any benefit to using a float opposed to a double?

ylun.ca
  • 2,504
  • 7
  • 26
  • 47
  • 2
    'f' appended to a literal number just indicates that it's a float value (instead of a double). (It has nothing to do with JBox - it's just Java syntax) – aryn.galadar Mar 04 '14 at 19:15

2 Answers2

2

It is no unit, f indicates that the number is a float.

If you just write 0.1 for example, you will get an error because 0.1 will be parsed as a double value. The compiler needs the instruction that the value is desired a float. That's what's the f for.

See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

exception1
  • 1,239
  • 8
  • 17
  • I have tested your assertion that leaving out "f" will produce an error and the program continues to run. – ylun.ca Mar 04 '14 at 19:22
  • @ylun The instruction `float x = 3.1;` should not compile; however you can use also `3.1` when the value is not explicitely a float value, then it is compiled as a `double`. – exception1 Mar 04 '14 at 19:28
  • `jDef.maxMotorTorque = 45;` What is your opinion on this code? I feel confused as to why it must be defined to a float. – ylun.ca Mar 04 '14 at 19:31
  • 1
    So then I misunderstood you, sorry. What I wrote is valid for decimal (floating point) numbers. If you have an integer ("whole number") as `45` it is treated as an integer value (since it has no floating point `.`). Integers can be casted to floats without problems since your value can't lose presision. Therefore you can omit the `f` in this case. – exception1 Mar 04 '14 at 19:38
1

It indicates that the value is a float literal.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • What use does it have in the engine? Why would it be better to declare a float instead of a double? – ylun.ca Mar 04 '14 at 19:17
  • No idea. Possibly performance, possibly just an arbitrary or wrong design decision. – Matt Ball Mar 04 '14 at 19:18
  • why must a value be indicated as a float if the variable has been declared as a float (for example, this line: `float x = 20.0f`)? – ylun.ca Mar 04 '14 at 19:31