소스 검색

Ensure that encoded doubles have a decimal point

Numbers like 1.0 were being encoded as <<"1">> which can lead to a bit
of confusion. This merely checks if a decimial point exists and if not
it appends ".0" to the value.
pull/21/head 0.4.0
Paul J. Davis 13 년 전
부모
커밋
56e60433ae
3개의 변경된 파일31개의 추가작업 그리고 12개의 파일을 삭제
  1. +23
    -4
      c_src/encoder.c
  2. +7
    -7
      test/003-numbers.t
  3. +1
    -1
      test/005-arrays.t

+ 23
- 4
c_src/encoder.c 파일 보기

@ -385,15 +385,34 @@ enc_long(Encoder* e, ErlNifSInt64 val)
static inline int
enc_double(Encoder* e, double val)
{
char* start;
size_t len;
size_t i;
if(!enc_ensure(e, 32)) {
return 0;
}
//snprintf(&(e->p[e->i]), 31, "%0.20g", val);
sprintf(&(e->p[e->i]), "%.20g", val);
e->i += strlen(&(e->p[e->i]));
e->count++;
start = &(e->p[e->i]);
sprintf(start, "%0.20g", val);
len = strlen(start);
// Check if we have a decimal point
for(i = 0; i < len; i++) {
if(start[i] == '.' || start[i] == 'e' || start[i] == 'E')
goto done;
}
if(len > 29) return 0;
// Force a decimal point
start[len++] = '.';
start[len++] = '0';
done:
e->i += len;
e->count++;
return 1;
}

+ 7
- 7
test/003-numbers.t 파일 보기

@ -35,22 +35,22 @@ good() ->
1234567890123456789012345012,
<<"1234567890123456789012345012">>
},
{<<"1.0">>, 1.0},
{
<<"0.000000000000000000000000000000000001">>,
1.0E-36,
<<"9.9999999999999994104e-37">>
},
{<<"1.0">>, 1.0, <<"1">>},
{<<"0.75">>, 0.75},
{<<"2.0123456789">>, 2.0123456789, <<"2.0123456789000000455">>},
{<<"2.4234324E24">>, 2.4234324E24, <<"2.4234323999999998107e+24">>},
{<<"-3.1416">>, -3.1416, <<"-3.1415999999999999481">>},
{<<"1E4">>, 10000.0, <<"10000">>},
{<<"1.0E+01">>, 10.0, <<"10">>},
{<<"1e1">>, 10.0, <<"10">>},
{<<"3.0E2">>, 300.0, <<"300">>},
{<<"0E3">>, 0.0, <<"0">>},
{<<"1.5E3">>, 1500.0, <<"1500">>},
{<<"1E4">>, 10000.0, <<"10000.0">>},
{<<"1.0E+01">>, 10.0, <<"10.0">>},
{<<"1e1">>, 10.0, <<"10.0">>},
{<<"3.0E2">>, 300.0, <<"300.0">>},
{<<"0E3">>, 0.0, <<"0.0">>},
{<<"1.5E3">>, 1500.0, <<"1500.0">>},
{<<"2.5E-1">>, 0.25, <<"0.25">>},
{<<"-0.325E+2">>, -32.5, <<"-32.5">>}
].

+ 1
- 1
test/005-arrays.t 파일 보기

@ -17,7 +17,7 @@ good() ->
{<<"[\t[\n]\r]">>, [[]], <<"[[]]">>},
{<<"[\t123, \r true\n]">>, [123, true], <<"[123,true]">>},
{<<"[1,\"foo\"]">>, [1, <<"foo">>]},
{<<"[11993444355.0,1]">>, [11993444355.0,1], <<"[11993444355,1]">>},
{<<"[11993444355.0,1]">>, [11993444355.0,1]},
{
<<"[\"\\u00A1\",\"\\u00FC\"]">>,
[<<194, 161>>, <<195, 188>>],

불러오는 중...
취소
저장