Why float number is so fast in Objective-C?

Discussion in 'iOS Programming' started by mikezang, Nov 13, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    Before development for iPhone/iPad, It is said as possible as not use float number for calculation, but I found all sample source for iPhone/iPad use float all time.

    For example, when create a view using CGRectMake(0.0, 0.0, 123.0, 824.0) or CGRectMake(0.0f, 0.0f, 123.0f, 824.0f), maybe I am a newbie, I still like to use CGRectMake(0, 0, 123, 824), I have two questions:

    1. Do those methods above return the same result?

    2. Why the float is So fast in iPhone/iPad or Mac?

    Sorry for very basic and general questions:confused:
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    1. The compiler can promote the integer values to floats without losing precision so it will silently do so.

    2. The ARM CPU does floating point numbers in hardware, as do most modern CPU's.
     
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    CGRectMake's parameters are defined as floats, so whether you say 123.0 or 123.0f or 123 when you call the function, the numbers will be stored in and operated on in floating point format.

    Floating point operations are fast on any modern CPU, but integer operations are always likely to be even faster. Of course there are some cases where you just can't use integers, so floats are used instead. Unless you are doing floating point math in long, tight loops, you are unlikely to notice that float processing is slower than integer processing.
     
  4. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    Thanks for your explanation, so I will keep to use integer on my way:)
     

Share This Page