Converting NSNumber to JSON representation #113

Closed
felipebvalio opened this Issue Dec 1, 2011 · 2 comments

2 participants

@felipebvalio

I am using the framework version 3.1, and found an issue about converting a NSNumber with float/double to JSON representation. Some dirty decimal cases are added at rightmost digits, like 4.4 being converted to 4.4000000953674316

In SBJsonStreamWriter class, the NSNumber is being forced to be represented with 17 decimal cases with the code:

snprintf(num, sizeof num, "%.17g", [number doubleValue]);

As far as I know, double has 14 cases and float, 7. Moreover, is being used ever double, even when passing a float NSNumber.
Replacing the "%.17g" by "%g" resolved my problems.

@stig
Owner
@stig
Owner

This is just how IEEE floating point works, I'm afraid.

@stig stig added a commit that closed this issue Jun 22, 2013
@stig Move to less surprising handling of floating point numbers - fixes #113
…, #128 & #164

This removes the last trace of NSDecimalNumber, which has some surprising behaviour. It allows more precision but less range than double. This required:

* Removing tests for real numbers that cannot be represented accurately using IEEE floating point.
* Avoid rounding issues in tests when all we want is to make sure exponent is handled.

For clarity this also renamed tests for (positive and negative) zero for real numbers.
93a3c26
@stig stig closed this in 93a3c26 Jun 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment