Browse files

use binaries for float tests

  • Loading branch information...
1 parent c972e5e commit c54a8edb569f613434899c280c22f806aef24811 @etrepum etrepum committed Jan 2, 2008
Showing with 16 additions and 5 deletions.
  1. +4 −1 src/mochijson2.erl
  2. +12 −4 src/mochinum.erl
View
5 src/mochijson2.erl
@@ -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) ->
View
16 src/mochinum.erl
@@ -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.