integrates nicefloats into jsx
This commit is contained in:
parent
0c3dbaab6c
commit
32c9a1f00f
11 changed files with 39 additions and 180 deletions
21
deps/nicefloats/LICENSE
vendored
21
deps/nicefloats/LICENSE
vendored
|
@ -1,21 +0,0 @@
|
||||||
%% The MIT License
|
|
||||||
|
|
||||||
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
|
||||||
|
|
||||||
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
%% of this software and associated documentation files (the "Software"), to deal
|
|
||||||
%% in the Software without restriction, including without limitation the rights
|
|
||||||
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
%% copies of the Software, and to permit persons to whom the Software is
|
|
||||||
%% furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
%% The above copyright notice and this permission notice shall be included in
|
|
||||||
%% all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
%% THE SOFTWARE.
|
|
29
deps/nicefloats/README.markdown
vendored
29
deps/nicefloats/README.markdown
vendored
|
@ -1,29 +0,0 @@
|
||||||
nicefloats (v0.9.0)
|
|
||||||
===================
|
|
||||||
|
|
||||||
an erlang implementation of nice decimal representations of floating point numbers, based on "Printing FLoating-Point Numbers Quickly and Accurately" by Burger & Dybvig
|
|
||||||
|
|
||||||
|
|
||||||
### usage ###
|
|
||||||
|
|
||||||
`nicefloats:format(Float)`
|
|
||||||
|
|
||||||
|
|
||||||
### installation ###
|
|
||||||
|
|
||||||
`make` or `./rebar compile` to build nicefloats
|
|
||||||
|
|
||||||
`make test` or `./rebar eunit` to run tests
|
|
||||||
|
|
||||||
|
|
||||||
### notes ###
|
|
||||||
|
|
||||||
this project was originally a part of [jsx][1], and remnants of that projects history may linger. try to ignore it.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[1]: http://www.github.com/talentdeficit/jsx/
|
|
33
deps/nicefloats/doc/overview.edoc
vendored
33
deps/nicefloats/doc/overview.edoc
vendored
|
@ -1,33 +0,0 @@
|
||||||
@author Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
|
||||||
@copyright 2010 Alisdair Sullivan
|
|
||||||
@version really, really beta
|
|
||||||
@title nicefloats
|
|
||||||
@doc nicefloats is an implementation of the algorithms for pretty printing floating point numbers detailed in "Printing FLoating-Point Numbers Quickly and Accurately" by Burger & Dybvig
|
|
||||||
|
|
||||||
|
|
||||||
== contents ==
|
|
||||||
|
|
||||||
<ol>
|
|
||||||
<li>{@section introduction}</li>
|
|
||||||
<li>{@section features}</li>
|
|
||||||
<li>{@section usage}</li>
|
|
||||||
<li>{@section contributing}</li>
|
|
||||||
</ol>
|
|
||||||
|
|
||||||
|
|
||||||
== introduction ==
|
|
||||||
|
|
||||||
nicefloats quickly and accurately outputs decimal representations of floating point numbers that, when read back (with `erlang:list_to_float/1' or similar), result in the same floating point value
|
|
||||||
|
|
||||||
|
|
||||||
== features ==
|
|
||||||
|
|
||||||
nicefloats currently only outputs the shortest possible representation of floating point numbers in base 10.
|
|
||||||
|
|
||||||
== usage ==
|
|
||||||
|
|
||||||
`nicefloats:format(Float).'
|
|
||||||
|
|
||||||
== contributing ==
|
|
||||||
|
|
||||||
nicefloats is available on <a href="http://github.com/talentdeficit/nicefloats">github</a>. users are encouraged to fork, edit and make pull requests
|
|
14
deps/nicefloats/ebin/nicefloats.app
vendored
14
deps/nicefloats/ebin/nicefloats.app
vendored
|
@ -1,14 +0,0 @@
|
||||||
{application, nicefloats,
|
|
||||||
[
|
|
||||||
{description, "'nice' decimal representations of floating point numbers"},
|
|
||||||
{vsn, "0.9.0"},
|
|
||||||
{modules, [
|
|
||||||
nicefloats
|
|
||||||
]},
|
|
||||||
{registered, []},
|
|
||||||
{applications, [
|
|
||||||
kernel,
|
|
||||||
stdlib
|
|
||||||
]},
|
|
||||||
{env, []}
|
|
||||||
]}.
|
|
8
deps/nicefloats/makefile
vendored
8
deps/nicefloats/makefile
vendored
|
@ -1,8 +0,0 @@
|
||||||
compile:
|
|
||||||
./rebar compile
|
|
||||||
|
|
||||||
test:
|
|
||||||
./rebar eunit
|
|
||||||
|
|
||||||
clean:
|
|
||||||
./rebar clean
|
|
BIN
deps/nicefloats/rebar
vendored
BIN
deps/nicefloats/rebar
vendored
Binary file not shown.
2
deps/nicefloats/rebar.config
vendored
2
deps/nicefloats/rebar.config
vendored
|
@ -1,2 +0,0 @@
|
||||||
%% uncomment to get verbose output from test suite
|
|
||||||
%% {eunit_opts, [verbose]}.
|
|
|
@ -1,12 +1,6 @@
|
||||||
{lib_dirs, ["deps"]}.
|
|
||||||
|
|
||||||
{sub_dirs, [
|
|
||||||
"deps/nicefloats"
|
|
||||||
]}.
|
|
||||||
|
|
||||||
%% edit eunit_test_path if you want to run your own tests, use "../" not "./" as
|
%% edit eunit_test_path if you want to run your own tests, use "../" not "./" as
|
||||||
%% rebar changes to working dir to .eunit when running tests
|
%% rebar changes to working dir to .eunit when running tests
|
||||||
{eunit_compile_opts, [{d, eunit_test_path, "../test/cases"}]}.
|
{eunit_compile_opts, [{d, eunit_test_path, "../test/cases"}]}.
|
||||||
|
|
||||||
%% uncomment to get verbose output from test suite
|
%% uncomment to get verbose output from test suite
|
||||||
%% {eunit_opts, [verbose]}.
|
{eunit_opts, [verbose]}.
|
||||||
|
|
|
@ -11,7 +11,8 @@
|
||||||
jsx_utf32le,
|
jsx_utf32le,
|
||||||
jsx_eep0018,
|
jsx_eep0018,
|
||||||
jsx_format,
|
jsx_format,
|
||||||
jsx_verify
|
jsx_verify,
|
||||||
|
jsx_utils
|
||||||
]},
|
]},
|
||||||
{registered, []},
|
{registered, []},
|
||||||
{applications, [
|
{applications, [
|
||||||
|
|
|
@ -221,7 +221,7 @@ list_to_events([], Acc) ->
|
||||||
term_to_event(List) when is_list(List) ->
|
term_to_event(List) when is_list(List) ->
|
||||||
term_to_events(List);
|
term_to_events(List);
|
||||||
term_to_event(Float) when is_float(Float) ->
|
term_to_event(Float) when is_float(Float) ->
|
||||||
[{float, nicefloats:format(Float)}];
|
[{float, jsx_utils:nice_decimal(Float)}];
|
||||||
term_to_event(Integer) when is_integer(Integer) ->
|
term_to_event(Integer) when is_integer(Integer) ->
|
||||||
[{integer, erlang:integer_to_list(Integer)}];
|
[{integer, erlang:integer_to_list(Integer)}];
|
||||||
term_to_event(String) when is_binary(String) ->
|
term_to_event(String) when is_binary(String) ->
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
%% The MIT License
|
%% The MIT License
|
||||||
|
|
||||||
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
||||||
%% 2007 Bob Ippolito <bob@mochimedia.com>
|
|
||||||
|
|
||||||
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
%% of this software and associated documentation files (the "Software"), to deal
|
%% of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -22,6 +21,17 @@
|
||||||
%% THE SOFTWARE.
|
%% THE SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
|
-module(jsx_utils).
|
||||||
|
|
||||||
|
-export([nice_decimal/1]).
|
||||||
|
|
||||||
|
|
||||||
|
-ifdef(TEST).
|
||||||
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
-endif.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%% conversion of floats to 'nice' decimal output. erlang's float implementation
|
%% conversion of floats to 'nice' decimal output. erlang's float implementation
|
||||||
%% is almost but not quite ieee 754. it converts negative zero to plain zero
|
%% is almost but not quite ieee 754. it converts negative zero to plain zero
|
||||||
%% silently, and throws exceptions for any operations that would produce NaN
|
%% silently, and throws exceptions for any operations that would produce NaN
|
||||||
|
@ -30,65 +40,26 @@
|
||||||
%% operations produce badarg exceptions. with that in mind, this function
|
%% operations produce badarg exceptions. with that in mind, this function
|
||||||
%% makes no attempt to handle special values (except for zero)
|
%% makes no attempt to handle special values (except for zero)
|
||||||
|
|
||||||
%% although this is a from scratch implementation of burger & dybvig's algorithm
|
|
||||||
%% bob ippolito's implementation in mochiweb did provide signifigant guidance
|
|
||||||
%% and is reflected in the choice of license and copyright attribution
|
|
||||||
|
|
||||||
|
|
||||||
-module(nicefloats).
|
|
||||||
|
|
||||||
-export([format/1, format/2]).
|
|
||||||
|
|
||||||
|
|
||||||
-ifdef(TEST).
|
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
|
||||||
-endif.
|
|
||||||
|
|
||||||
|
|
||||||
-record(opts, {
|
|
||||||
exp_limit = 6,
|
|
||||||
base = 10
|
|
||||||
}).
|
|
||||||
|
|
||||||
|
|
||||||
-spec format(Float::float()) -> string().
|
|
||||||
|
|
||||||
format(Float) when is_float(Float) ->
|
|
||||||
format(Float, []).
|
|
||||||
|
|
||||||
-spec format(Float::float(), OptsList::list()) -> string().
|
|
||||||
|
|
||||||
format(Float, OptsList) when is_float(Float) ->
|
|
||||||
nice_decimal(Float, parse_opts(OptsList)).
|
|
||||||
|
|
||||||
|
|
||||||
parse_opts(OptsList) ->
|
|
||||||
parse_opts(OptsList, #opts{}).
|
|
||||||
|
|
||||||
parse_opts([{exp_limit, Val}|Rest], Opts) when is_integer(Val), Val >= 0 ->
|
|
||||||
parse_opts(Rest, Opts#opts{exp_limit = Val});
|
|
||||||
parse_opts([{base, N}|Rest], Opts) when is_integer(N), N >= 2, N =< 16 ->
|
|
||||||
parse_opts(Rest, Opts#opts{base = N});
|
|
||||||
parse_opts([], Opts) ->
|
|
||||||
Opts;
|
|
||||||
parse_opts(_, _) ->
|
|
||||||
{error, badarg}.
|
|
||||||
|
|
||||||
|
|
||||||
%% algorithm from "Printing Floating-Point Numbers Quickly and Accurately" by
|
%% algorithm from "Printing Floating-Point Numbers Quickly and Accurately" by
|
||||||
%% Burger & Dybvig
|
%% Burger & Dybvig
|
||||||
nice_decimal(0.0, _) -> "0.0";
|
|
||||||
nice_decimal(Num, Opts) ->
|
|
||||||
|
-spec nice_decimal(Float::float()) -> string().
|
||||||
|
|
||||||
|
nice_decimal(0.0) -> "0.0";
|
||||||
|
nice_decimal(Num) ->
|
||||||
{F, E} = extract(<<Num:64/float>>),
|
{F, E} = extract(<<Num:64/float>>),
|
||||||
{R, S, MP, MM} = initial_vals(F, E),
|
{R, S, MP, MM} = initial_vals(F, E),
|
||||||
K = ceiling((math:log(abs(Num)) / math:log(Opts#opts.base)) - 1.0e-10),
|
K = ceiling((math:log(abs(Num)) / math:log(10)) - 1.0e-10),
|
||||||
Round = F band 1 =:= 0,
|
Round = F band 1 =:= 0,
|
||||||
{Dpoint, Digits} = scale(R, S, MP, MM, K, Opts#opts.base, Round),
|
{Dpoint, Digits} = scale(R, S, MP, MM, K, 10, Round),
|
||||||
if Num >= 0 -> digits_to_list(Dpoint, Digits)
|
if Num >= 0 -> digits_to_list(Dpoint, Digits)
|
||||||
; Num < 0 -> "-" ++ digits_to_list(Dpoint, Digits)
|
; Num < 0 -> "-" ++ digits_to_list(Dpoint, Digits)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
%% internal functions
|
||||||
|
|
||||||
extract(<<_:1, 0:11, Frac:52>>) -> {Frac, -1074};
|
extract(<<_:1, 0:11, Frac:52>>) -> {Frac, -1074};
|
||||||
extract(<<_:1, Exp:11, Frac:52>>) -> {Frac + (1 bsl 52), Exp - 1075}.
|
extract(<<_:1, Exp:11, Frac:52>>) -> {Frac + (1 bsl 52), Exp - 1075}.
|
||||||
|
|
||||||
|
@ -206,36 +177,36 @@ to_ascii(X) -> [X + 48]. %% ascii "1" is [49], "2" is [50], etc...
|
||||||
|
|
||||||
nice_decimal_test_() ->
|
nice_decimal_test_() ->
|
||||||
[
|
[
|
||||||
{"0.0", ?_assert(format(0.0) =:= "0.0")},
|
{"0.0", ?_assert(nice_decimal(0.0) =:= "0.0")},
|
||||||
{"1.0", ?_assert(format(1.0) =:= "1.0")},
|
{"1.0", ?_assert(nice_decimal(1.0) =:= "1.0")},
|
||||||
{"-1.0", ?_assert(format(-1.0) =:= "-1.0")},
|
{"-1.0", ?_assert(nice_decimal(-1.0) =:= "-1.0")},
|
||||||
{"3.1234567890987654321",
|
{"3.1234567890987654321",
|
||||||
?_assert(
|
?_assert(
|
||||||
format(3.1234567890987654321) =:= "3.1234567890987655")
|
nice_decimal(3.1234567890987654321) =:= "3.1234567890987655")
|
||||||
},
|
},
|
||||||
{"1.0e23", ?_assert(format(1.0e23) =:= "1.0e23")},
|
{"1.0e23", ?_assert(nice_decimal(1.0e23) =:= "1.0e23")},
|
||||||
{"0.3", ?_assert(format(3.0/10.0) =:= "0.3")},
|
{"0.3", ?_assert(nice_decimal(3.0/10.0) =:= "0.3")},
|
||||||
{"0.0001", ?_assert(format(0.0001) =:= "1.0e-4")},
|
{"0.0001", ?_assert(nice_decimal(0.0001) =:= "1.0e-4")},
|
||||||
{"0.00000001", ?_assert(format(0.00000001) =:= "1.0e-8")},
|
{"0.00000001", ?_assert(nice_decimal(0.00000001) =:= "1.0e-8")},
|
||||||
{"1.0e-323", ?_assert(format(1.0e-323) =:= "1.0e-323")},
|
{"1.0e-323", ?_assert(nice_decimal(1.0e-323) =:= "1.0e-323")},
|
||||||
{"1.0e308", ?_assert(format(1.0e308) =:= "1.0e308")},
|
{"1.0e308", ?_assert(nice_decimal(1.0e308) =:= "1.0e308")},
|
||||||
{"min normalized float",
|
{"min normalized float",
|
||||||
?_assert(
|
?_assert(
|
||||||
format(math:pow(2, -1022)) =:= "2.2250738585072014e-308"
|
nice_decimal(math:pow(2, -1022)) =:= "2.2250738585072014e-308"
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
{"max normalized float",
|
{"max normalized float",
|
||||||
?_assert(
|
?_assert(
|
||||||
format((2 - math:pow(2, -52)) * math:pow(2, 1023))
|
nice_decimal((2 - math:pow(2, -52)) * math:pow(2, 1023))
|
||||||
=:= "1.7976931348623157e308"
|
=:= "1.7976931348623157e308"
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
{"min denormalized float",
|
{"min denormalized float",
|
||||||
?_assert(format(math:pow(2, -1074)) =:= "5.0e-324")
|
?_assert(nice_decimal(math:pow(2, -1074)) =:= "5.0e-324")
|
||||||
},
|
},
|
||||||
{"max denormalized float",
|
{"max denormalized float",
|
||||||
?_assert(
|
?_assert(
|
||||||
format((1 - math:pow(2, -52)) * math:pow(2, -1022))
|
nice_decimal((1 - math:pow(2, -52)) * math:pow(2, -1022))
|
||||||
=:= "2.225073858507201e-308"
|
=:= "2.225073858507201e-308"
|
||||||
)
|
)
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue