“f” after number

后端 未结 10 684
醉酒成梦
醉酒成梦 2020-11-27 11:02

What does the f after the numbers indicate? Is this from C or Objective-C? Is there any difference in not adding this to a constant number?

CGRec         


        
相关标签:
10条回答
  • 2020-11-27 12:00

    From C. It means float literal constant. You can omit both "f" and ".0" and use ints in your example because of implicit conversion of ints to floats.

    0 讨论(0)
  • 2020-11-27 12:04

    Sometimes there is a difference.

    float f = 0.3; /* OK, throw away bits to convert 0.3 from double to float */
    assert ( f == 0.3 ); /* not OK, f is converted from float to double
       and the value of 0.3 depends on how many bits you use to represent it. */
    assert ( f == 0.3f ); /* OK, comparing two floats, although == is finicky. */
    
    0 讨论(0)
  • 2020-11-27 12:06

    It is almost certainly from C and reflects the desire to use a 'float' rather than a 'double' type. It is similar to suffixes such as L on numbers to indicate they are long integers. You can just use integers and the compiler will auto convert as appropriate (for this specific scenario).

    0 讨论(0)
  • 2020-11-27 12:07

    The f that you are talking about is probably meant to tell the compiler that it's working with a float. When you omit the f, it is usually translated to a double.

    Both are floating point numbers, but a float uses less bits (thus smaller and less precise) than a double.

    0 讨论(0)
提交回复
热议问题