Which is best for iOS, double or float?

I know CGFloat is double on 64-bit devices and float on 32-bit devices. However, my understanding is that ARM 64-bit only refers to integers and pointers. The Floating point unit has been capable of handling doubles long before iOS devices went 64-bit. The floating point unit can operate on 4 32-bit floats at once, or 2 doubles or one 128-bit float. So, if you use doubles instead of floats you are (theoretically) halfing the processing power.

Is this correct?

I haven't been able to get much detailed information on Apple's implementation of the ARM floating point processor, so I might have got some of the details wrong. Please, feel free to correct 😉

Answered by DTS Engineer in 89329022

Well, what I really want to know is if you gain anything from using floats instead of doubles on iOS devices.

Hmmm. I was hoping for a more in-depth description of your overall problem. For example:

  • How many numbers are you working with?

  • How do they flow in and out of memory? And in and out of the CPU?

  • When you operate on those numbers, how many are you operating on in parallel? And how complex are those mathematical operations?

  • Are their any GPU entanglements?

The most obvious difference between floats and doubles is their actual size in memory. If you’re working with a large array of numbers, the float version will be half the size of the double version, which is clearly a good thing. Moreover, if you’re doing simple operations on large arrays of numbers, it’s likely that those operations will be memory bound, in which case you’ll get twice the throughput with floats.

OTOH, if you’re doing simple day-to-day arithmetic—calculating the value for a progress bar, say—it’s generally OK to just default to using a double.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I know CGFloat is double on 64-bit devices and float on 32-bit devices.

Just FYI, that decision was inherited from OS X, where it was made based on the performance characteristics of early PowerPC CPUs. Thus, you shouldn’t draw any conclusions about modern CPUs from that factoid.

As to your overall question, it’s hard to answer without more information about your goals. Perhaps you can offer up some details of the problem you’re trying to solve.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Well, what I really want to know is if you gain anything from using floats instead of doubles on iOS devices. My theory is that you potentially do, for the reasons I mentioned earlier.

That's just a repeat of your original question - can you provide a specific use case? Otherwise, in general, you may have your answer.

From Green Eggs and Ham..."Try it. Try it."


On the simulator:

    double XX;
    float YY;
    XX=sqrt(2.0);
    YY=sqrt(2.0);
    XX=(XX*XX-2.0)*1000000.;
    YY=(YY*YY-2.0)*1000000.;
    NSLog(@"the values are %f   %f",XX,YY);
  
    XX=sqrtf(2.0);
    YY=sqrtf(2.0);
    XX=(XX*XX-2.0)*1000000.;
    YY=(YY*YY-2.0)*1000000.;
    NSLog(@"the values are %f   %f",XX,YY);


2015-11-19 16:49:01.623 PrincetonRideShare[30245:3070429] the values are 0.000000 -0.119209

2015-11-19 16:49:01.623 PrincetonRideShare[30245:3070429] the values are -0.068457 -0.119209

Accepted Answer

Well, what I really want to know is if you gain anything from using floats instead of doubles on iOS devices.

Hmmm. I was hoping for a more in-depth description of your overall problem. For example:

  • How many numbers are you working with?

  • How do they flow in and out of memory? And in and out of the CPU?

  • When you operate on those numbers, how many are you operating on in parallel? And how complex are those mathematical operations?

  • Are their any GPU entanglements?

The most obvious difference between floats and doubles is their actual size in memory. If you’re working with a large array of numbers, the float version will be half the size of the double version, which is clearly a good thing. Moreover, if you’re doing simple operations on large arrays of numbers, it’s likely that those operations will be memory bound, in which case you’ll get twice the throughput with floats.

OTOH, if you’re doing simple day-to-day arithmetic—calculating the value for a progress bar, say—it’s generally OK to just default to using a double.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks eskimo, that answers my question.

I develop mainly games. It's hard to give a single use case since floats are used ubiquitously throughout my code - but I would say it's just mostly simple operations for geometry calculations.

My understanding is that most games use float because of GPU integration issues (most GPUs deal well with float and not well with double). However, I’m definitely not a game programmer so I have little concrete experience in this area.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

sorry to disturb this thread - I am desperately looking for a button to open a new topic on this website!

any clues?


thanks,

Klaus

😝

It's right there on the welcome screen. Type in something in the search box

if nothing is found, it envites you to create a new topic. Or, you can go into

specific sections and you'll find a similar search box. There is also a

Start A Discussion link on the right side of the page within the individual forum

sections.

It's used everywhere, not just the GPU related parts

Which is best for iOS, double or float?
 
 
Q