Printing floats with Erlang
mochiweb,erlang
ThefloatprintingoptionsthatshipwithErlangallsuck.Youeitherdon'thavesufficientprecision,oryouhaveanextremelyverboserepresentation:
1> io:format("~w ~.18g ~g ~s~n", [0.1234567, 0.1234567, 0.1234567, float_to_list(0.1234567)]). 0.123457 0.123456700000000003 0.123457 1.23456700000000002548e-01 2> io:format("~w ~.18g ~g ~s~n", [0.1, 0.1, 0.1, float_to_list(0.1)]). 0.100000 0.100000000000000006 0.100000 1.00000000000000005551e-01
mochiwebincludesanimplementationofthealgorithmfromthe"PrintingFloating-PointNumbersQuicklyandAccurately"paperinthemochinummodule(whichisstandalone,ifyoujustwanttouseitwithouttherestofmochiweb):
3> io:format("~s~n", [mochinum:digits(0.1234567)]). 0.1234567 4> io:format("~s~n", [mochinum:digits(0.1)]). 0.1
相关推荐
chenpro 2020-08-09
NVEFLY 2020-07-04
liym 2020-06-21
OnMyHeart 2020-06-06
天空windy 2020-06-03
87447007 2020-05-16
OnMyHeart 2020-05-09
NVEFLY 2020-04-17
M守护神 2020-03-28
大史哥哥 2020-03-07
wbingyang 2020-02-27
liym 2020-02-22
zhoucheng0 2020-02-19
wbingyang 2020-02-14
OnMyHeart 2020-01-14
OnMyHeart 2020-01-08
大史哥哥 2019-12-31
wbingyang 2019-12-31