views:

74

answers:

2

I have an CGFloat property and sometimes I get a return value of type Float64 or also of type Float32. Could I store both safely to CGFloat?

+1  A: 

From the headers:

// CGBase.h
typedef float CGFloat;

// MacTypes.h
typedef float               Float32;
typedef double              Float64;

So CGFloat and Float32 are both floats while Float64 is a double so you would lose precision.

(Edit to clarify: this is for 32 bit systems such as the iPhone. If you are building for 64 bit, CGFloat is defined as a double.)

Ole Begemann
+1  A: 

It's best practice to always try and store scalar values in the same type as you received them because the precision of scalar types changes with the hardware.

CGFloat isn't always guaranteed to be the same size on all current and future hardware. If you substitute another type for it or use it to store another type, your code made break somewhere down the road.

You might gain or lose precision when a new iPhone/iPad comes out or the code might break if you try to port it to Macs.

TechZen