You shouldn't convert between doubles and strings all the time, since it tends to lose accuracy (in general, you have to be very careful if you don't want to lose accuracy).
For example, nextafter(1.0,2.0)
returns the smallest double larger than 1 (exactly 1.0000000000000002220446049250313080847263336181640625, but we're happy with 1.0000000000000002). Formatting with "%g" just returns "1".
We'd expect NSNumber to do the right thing, but [[NSNumber numberWithDouble:nextafter(1,2)] stringValue]
also returns "1". According to the docs, -[NSNumber stringValue]
calls [self descriptionWithLocale:nil]
which formats it with "%0.16g". That's not enough. We get the right answer by formatting with "%.17g", though. I think "%.17g" is enough provided that doubles are IEEE 754 64-bit doubles and the libraries are working properly, but there's no guarantee.
At the end of the day, there's no guarantee that [[NSString stringWithFormat:@"%.17g",n] doubleValue] == n
.