pir800
pir800

Reputation: 1011

NSNumbers are inaccurate when retrieved using core data

This is a little odd to me, but when I perform basic division on a float I get inaccurate approximations. This isn't a huge deal because I can truncate the number, but I'm just curious as to why NSNumbers behave this way. Sample code included...

NSString *stringNumber = @"2782";
float ratio = [stringNumber floatValue]/10000;
NSNumber *sizeRatio = [NSNumber numberWithFloat:ratio];

//core data save...

on subsequent loads the number reads: 0.2782000005245209

Any ideas? Google hasn't been very helpful because it keeps showing me results for "number" instead of "nsnumber"

Upvotes: 0

Views: 203

Answers (2)

Mike Hay
Mike Hay

Reputation: 2856

It turns out using floats results in rather inaccurate results on iOS. I've had much better success using doubles instead.

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304413

This is normal for any floating point numbers

See wikipedia entry

Upvotes: 4

Related Questions