Skip to content

Commit

Permalink
use binaries for float tests
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Jan 2, 2008
1 parent c972e5e commit c54a8ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/mochijson2.erl
Expand Up @@ -93,8 +93,11 @@ json_encode(false, _State) ->
<<"false">>;
json_encode(null, _State) ->
<<"null">>;
json_encode(I, _State) when is_integer(I) ->
json_encode(I, _State) when is_integer(I) andalso I >= -2147483648 andalso I =< 2147483647 ->
%% Anything outside of 32-bit integers should be encoded as a float
integer_to_list(I);
json_encode(I, _State) when is_integer(I) ->
mochinum:digits(float(I));
json_encode(F, _State) when is_float(F) ->
mochinum:digits(F);
json_encode(S, State) when is_binary(S); is_atom(S) ->
Expand Down
16 changes: 12 additions & 4 deletions src/mochinum.erl
Expand Up @@ -271,11 +271,19 @@ test_frexp() ->
%% negative one
{-0.5, 1} = frexp(-1.0),
%% small denormalized number
{0.5, -1073} = frexp(4.94065645841246544177e-324),
%% 4.94065645841246544177e-324
<<SmallDenorm/float>> = <<0,0,0,0,0,0,0,1>>,
{0.5, -1073} = frexp(SmallDenorm),
%% large denormalized number
{0.99999999999999978, -1022} = frexp(2.22507385850720088902e-308),
%% 2.22507385850720088902e-308
<<BigDenorm/float>> = <<0,15,255,255,255,255,255,255>>,
{0.99999999999999978, -1022} = frexp(BigDenorm),
%% small normalized number
{0.5, -1021} = frexp(2.22507385850720138309e-308),
%% 2.22507385850720138309e-308
<<SmallNorm/float>> = <<0,16,0,0,0,0,0,0>>,
{0.5, -1021} = frexp(SmallNorm),
%% large normalized number
{0.99999999999999989, 1024} = frexp(1.79769313486231570815e+308),
%% 1.79769313486231570815e+308
<<LargeNorm/float>> = <<127,239,255,255,255,255,255,255>>,
{0.99999999999999989, 1024} = frexp(LargeNorm),
ok.

0 comments on commit c54a8ed

Please sign in to comment.