tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit 3d7acc24cd060bad84fa3e7dbf008409925bb863
parent cb33e7a449896ee5a5e2b4da4885b7a4daddc852
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Fri, 11 Jan 2019 17:59:16 -0800

Print 16 significant figures of decimals

Diffstat:
test.c | 54+++++++++++++++++++++++++++---------------------------
tib/math.c | 4++--
tisp.c | 4+++-
3 files changed, 32 insertions(+), 30 deletions(-)

diff --git a/test.c b/test.c @@ -34,33 +34,33 @@ tisp_test(Env env, const char *input, const char *expect) /* TODO mark and show which lines error-ed */ char *tests[][2] = { - { "self", NULL }, - { "1", "1" }, - { "2", "2" }, - { "0", "0" }, - { "30", "30" }, - { "12", "12" }, - { "-4", "-4" }, - { "-083", "-83" }, - { "-0", "0" }, - { "+4", "4" }, - { "+123", "123" }, - { "12.0", "12.0" }, - { "08", "8" }, - { "+12.34", "12.3" }, - { ".34", "0.3" }, - { "2.", "2.0" }, - { "1e0", "1" }, - { "1E+0", "1" }, - { "1e-0", "1" }, - { "1E4", "10000" }, - { ".1e-4", "0.0" }, - { "-5.0e006", "-5000000.0" }, - { "-5.E+16", "-50000000000000000.0" }, - { "-.05", "-0.1" }, - { "-.0", "-0.0" }, - { "-1.e6", "-1000000.0" }, - { "\"foo\"", "\"foo\"" }, + { "self", NULL }, + { "1", "1" }, + { "2", "2" }, + { "0", "0" }, + { "30", "30" }, + { "12", "12" }, + { "-4", "-4" }, + { "-083", "-83" }, + { "-0", "0" }, + { "+4", "4" }, + { "+123", "123" }, + { "12.0", "12.0" }, + { "08", "8" }, + { "+12.34", "12.34" }, + { ".34", "0.34" }, + { "2.", "2.0" }, + { "1e0", "1" }, + { "1E+0", "1" }, + { "1e-0", "1" }, + { "1E4", "10000" }, + { ".1e-4", "1e-05" }, + { "-5.0e006", "-5000000.0" }, + { "-5.E+16", "-5e+16" }, + { "-.05", "-0.05" }, + { "-.0", "-0.0" }, + { "-1.e6", "-1000000.0" }, + { "\"foo\"", "\"foo\"" }, { "\"foo bar\"", "\"foo bar\"" }, { "t", "t" }, { "()", "()" }, diff --git a/tib/math.c b/tib/math.c @@ -92,8 +92,8 @@ PRIM_COMPARE(gte, >=, ">=") void tib_env_math(Env env) { - tisp_env_add(env, "pi", mk_dub(3.1415926535)); - tisp_env_add(env, "e", mk_dub(2.7182818284)); + tisp_env_add(env, "pi", mk_dub(3.141592653589793)); + tisp_env_add(env, "e", mk_dub(2.718281828459045)); tisp_env_add(env, "+", mk_prim(prim_add)); tisp_env_add(env, "-", mk_prim(prim_sub)); diff --git a/tisp.c b/tisp.c @@ -614,7 +614,9 @@ tisp_print(FILE *f, Val v) fprintf(f, "%d", v->v.i); break; case DOUBLE: - fprintf(f, "%.1f", v->v.d); + fprintf(f, "%.16g", v->v.d); + if (v->v.d == (int)v->v.d) + fprintf(f, ".0"); break; case RATIO: fprintf(f, "%d/%d", v->v.r.num, v->v.r.den);