Tuesday, May 31, 2016

NSDecimalNumber and unsignedIntegerValue

So, if you have an NSDecimalNumber and you want to get the unsigned integer equivalent of it, you'd do [nsdn unsignedIntegerValue], right?

Wrong. Or, at least, risky.

I hit a bug in NSDecimalNumber this week, and I thought I'd share it as a playground, which you can download or read, but I'll summarize here.

Essentially, if you have an NSDecimalNumber containing a real number with a large mantissa and you call unsignedIntegerValue on it, you may get zero instead of the nearest integer. I assume it's a bug, rather than a design decision, but it's definitely risky to rely on unsignedIntegerValue here. If you  convert the value yourself (e.g. call floor(nsdn.doubleValue)) you'll be much better off.

I've filed a radar.

No comments:

Post a Comment