In 0.75f, what does f stand for?

PerPer
edited March 2017 in Programming Questions

Short question. What does the f stand for in a value like this?

0.75f

Tagged:

Answers

  • edited March 2017
    1. f -> float.
    2. d -> double.
    3. L -> long.

    PDE's pre-processor already suffixes any literal value w/ . and/or e in it w/ f if it doesn't have 1 already. L-)

  • PerPer
    edited March 2017

    Thanks. But whats the main point of defining 0.75 as a float? :)

    For example in a expression like this:

    float r;
    r *= 0.75f;
    

    compared to

    float r;
    r *= 0.75
    
  • I've already said: PDE's pre-processor already takes care of auto-suffixing fractional literals w/ f. :-<

  • @Per -- it has no effect, but it can add clarity for those reading the code, particularly if some variables are double and some are float, it says "I'm intentionally, explicitly making this a float, it isn't supposed to be a double or anything else."

    This is somewhat like saying "we will meet at 12:00A, midnight." 12:00A is already midnight, but you are underscoring it to make clear to the reader that this isn't a mistake and you didn't actually mean 12:00P (noon).

    In this case the reader is another programmer (or yourself, later) not the compiler.

  • It has little effect in Processing but if you more to a Java IDE such as Eclipse or Netbeans then numbers such as 0.75 will be treated as doubles. If the compiler is expecting a float then this will cause an error e.g. we have a method that has a float parameter e,g,

    void fooMethod(float n){
      // do something clever here
    }
    

    then call the method with

    fooMethod(0.75);

    will be accepted by Processing because it adds the 'f' but rejected in Eclipse because it is a double, a different data type. You would have to do

    fooMethod(0.75f);
    
Sign In or Register to comment.