Globalhawk
Globalhawk

Reputation: 148

When CGPointMake meets big number

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.

Upvotes: 1

Views: 528

Answers (3)

Rudolf Adamkovič
Rudolf Adamkovič

Reputation: 31486

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

Upvotes: 2

Christoph
Christoph

Reputation: 1525

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.

Upvotes: 1

Pierre
Pierre

Reputation: 11633

If you write :

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

Is it better ?

Upvotes: 0

Related Questions