1

I am trying to make a time based drawing. For example: define a point for later use.

CGPoint testPoint = CGPointMake(2341.2345, 1350046324.1234);

Then testPoint.y becomes 1350046336.00 which is not we put there. I am using Xcode 4.5. Any ideas? Thanks.

Globalhawk
  • 148
  • 2
  • 8

3 Answers3

2

CGPoint is defined in CGGeometry.h:

struct CGPoint {
  CGFloat x;
  CGFloat y;
};

On 32-bit systems CGFloat is a typedef to float. iPhone/iPad is a 32-bit system. The last piece of information - float numbers in C (Objective C is a strict superset of C):

Floating point numbers in C use IEEE 754 encoding. Because of this encoding, you can never guarantee that you will not have a change in your value.

You can read the entire discussion here: float vs. double precision

Community
  • 1
  • 1
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
1

CGPoint uses float (32bit) datatypes (at least on iOS6).

From the headers:

CGPointMake(CGFloat x, CGFloat y) ...

with

typedef CGFLOAT_TYPE CGFloat;

and

# define CGFLOAT_TYPE float

So this results in my test code:

CGPoint testPoint1 = CGPointMake(2341.2345, 1350046324.1234);
CGPoint testPoint2 = CGPointMake(2341.2345f, 1350046324.1234f);
double d = 1350046324.1234;
float f = 1350046324.1234;

NSLog(@"%f %f %f %f", testPoint1.y, testPoint2.y, d, f);

printing to the log:

1350046336.000000 1350046336.000000 1350046324.123400 1350046336.000000

So you just left the range of numbers where single precision floats are good enough. Thats all.

BTW.: I did think CGFloat is double before I stumbled upon your question.

Christoph
  • 1,525
  • 1
  • 16
  • 27
0

If you write :

CGPoint testPoint = CGPointMake(2341.2345f, 1350046324.1234f);

Is it better ?

Pierre
  • 10,593
  • 5
  • 50
  • 80