mirror of
https://github.com/ultrajson/ultrajson.git
synced 2024-05-29 02:26:09 +02:00
increased double precision
This commit is contained in:
parent
79e980641d
commit
a1fd2a582a
|
@ -61,7 +61,7 @@ tree doesn't have cyclic references.
|
|||
|
||||
// Max decimals to encode double floating point numbers with
|
||||
#ifndef JSON_DOUBLE_MAX_DECIMALS
|
||||
#define JSON_DOUBLE_MAX_DECIMALS 9
|
||||
#define JSON_DOUBLE_MAX_DECIMALS 15
|
||||
#endif
|
||||
|
||||
// Max recursion depth, default for encoder
|
||||
|
|
|
@ -58,7 +58,7 @@ typedef JSOBJ (*PFN_DECODER)( struct DecoderState *ds);
|
|||
|
||||
double createDouble(double intNeg, double intValue, double frcValue, int frcDecimalCount)
|
||||
{
|
||||
static const double g_pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
|
||||
static const double g_pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000};
|
||||
|
||||
return (intValue + (frcValue / g_pow10[frcDecimalCount])) * intNeg;
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ Copyright (c) 2007 Nick Galbreath -- nickg [at] modp [dot] com. All rights rese
|
|||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
static const double g_pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
|
||||
static const double g_pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000};
|
||||
static const char g_hexChars[] = "0123456789abcdef";
|
||||
static const char g_escapeChars[] = "0123456789\\b\\t\\n\\f\\r\\\"\\\\\\/";
|
||||
|
||||
|
@ -452,14 +452,14 @@ void Buffer_AppendLongUnchecked(JSONObjectEncoder *enc, JSINT64 value)
|
|||
int Buffer_AppendDoubleUnchecked(JSOBJ obj, JSONObjectEncoder *enc, double value)
|
||||
{
|
||||
/* if input is larger than thres_max, revert to exponential */
|
||||
const double thres_max = (double)(0x7FFFFFFF);
|
||||
const double thres_max = (double) 1e16 - 1;
|
||||
int count;
|
||||
double diff = 0.0;
|
||||
char* str = enc->offset;
|
||||
char* wstr = str;
|
||||
int whole;
|
||||
unsigned long long whole;
|
||||
double tmp;
|
||||
uint32_t frac;
|
||||
unsigned long long frac;
|
||||
int neg;
|
||||
double pow10;
|
||||
|
||||
|
@ -486,9 +486,9 @@ int Buffer_AppendDoubleUnchecked(JSOBJ obj, JSONObjectEncoder *enc, double value
|
|||
|
||||
pow10 = g_pow10[enc->doublePrecision];
|
||||
|
||||
whole = (int) value;
|
||||
whole = (unsigned long long) value;
|
||||
tmp = (value - whole) * pow10;
|
||||
frac = (uint32_t)(tmp);
|
||||
frac = (unsigned long long)(tmp);
|
||||
diff = tmp - frac;
|
||||
|
||||
if (diff > 0.5)
|
||||
|
@ -517,7 +517,7 @@ int Buffer_AppendDoubleUnchecked(JSOBJ obj, JSONObjectEncoder *enc, double value
|
|||
*/
|
||||
if (value > thres_max)
|
||||
{
|
||||
enc->offset += sprintf(str, "%e", neg ? -value : value);
|
||||
enc->offset += sprintf(str, "%.15e", neg ? -value : value);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
|
@ -56,11 +56,15 @@ class UltraJSONTests(TestCase):
|
|||
self.assertEquals(input, ujson.decode(output))
|
||||
|
||||
def test_doublePrecisionTest(self):
|
||||
input = 30.012345678
|
||||
output = ujson.encode(input, double_precision = 9)
|
||||
input = 30.012345678901234
|
||||
output = ujson.encode(input, double_precision = 15)
|
||||
self.assertEquals(input, json.loads(output))
|
||||
self.assertEquals(input, ujson.decode(output))
|
||||
|
||||
output = ujson.encode(input, double_precision = 9)
|
||||
self.assertEquals(round(input, 9), json.loads(output))
|
||||
self.assertEquals(round(input, 9), ujson.decode(output))
|
||||
|
||||
output = ujson.encode(input, double_precision = 3)
|
||||
self.assertEquals(round(input, 3), json.loads(output))
|
||||
self.assertEquals(round(input, 3), ujson.decode(output))
|
||||
|
@ -72,14 +76,14 @@ class UltraJSONTests(TestCase):
|
|||
def test_invalidDoublePrecision(self):
|
||||
input = 30.12345678901234567890
|
||||
output = ujson.encode(input, double_precision = 20)
|
||||
# should snap to the max, which is 9
|
||||
self.assertEquals(round(input, 9), json.loads(output))
|
||||
self.assertEquals(round(input, 9), ujson.decode(output))
|
||||
# should snap to the max, which is 15
|
||||
self.assertEquals(round(input, 15), json.loads(output))
|
||||
self.assertEquals(round(input, 15), ujson.decode(output))
|
||||
|
||||
output = ujson.encode(input, double_precision = -1)
|
||||
# also should snap to the max, which is 9
|
||||
self.assertEquals(round(input, 9), json.loads(output))
|
||||
self.assertEquals(round(input, 9), ujson.decode(output))
|
||||
# also should snap to the max, which is 15
|
||||
self.assertEquals(round(input, 15), json.loads(output))
|
||||
self.assertEquals(round(input, 15), ujson.decode(output))
|
||||
|
||||
# will throw typeError
|
||||
self.assertRaises(TypeError, ujson.encode, input, double_precision = '9')
|
||||
|
|
Loading…
Reference in New Issue