From abd8710841222b6bfb41133eb20e277777e06438 Mon Sep 17 00:00:00 2001 From: bumbread Date: Sat, 6 Aug 2022 11:06:03 +1100 Subject: [PATCH] Fix some printf bugs and document other --- src/fmt/gen_fmt.h | 4 ++-- test/math.c | 10 +++++----- todo | 2 ++ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/fmt/gen_fmt.h b/src/fmt/gen_fmt.h index e8362d2..d47edc4 100644 --- a/src/fmt/gen_fmt.h +++ b/src/fmt/gen_fmt.h @@ -773,8 +773,8 @@ static inline int pfx(_dtoa)( // Print decimal point if(decimal_point_n) out('.'); // Print fractional part that's made out of digits - int prec_digs = prec - mant_digits_n; - while(mant_digits_n) { + int prec_digs = prec < mant_digits_n ? prec : mant_digits_n; + while(mant_digits_n < prec_digs) { if(-exp >= mant_digits_n) { out('0'); } diff --git a/test/math.c b/test/math.c index a1ee11a..0e58bcc 100644 --- a/test/math.c +++ b/test/math.c @@ -106,15 +106,15 @@ int main() { if(fetestexcept(FE_INVALID)) printf(" FE_INVALID was raised\n"); printf("\n\n=== exp === \n"); - printf("exp(1) = %f\n", exp(1)); - printf("FV of $100, continuously compounded at 3%% for 1 year = %f\n", + printf("exp(1) = %e\n", exp(1)); + printf("FV of $100, continuously compounded at 3%% for 1 year = %e\n", 100*exp(0.03)); // special values - printf("exp(-0) = %f\n", exp(-0.0)); - printf("exp(-Inf) = %f\n", exp(-INFINITY)); + printf("exp(-0) = %e\n", exp(-0.0)); + printf("exp(-Inf) = %e\n", exp(-INFINITY)); //error handling errno = 0; feclearexcept(FE_ALL_EXCEPT); - printf("exp(710) = %f\n", exp(710)); + printf("exp(710) = %e\n", exp(710)); if(errno == ERANGE) printf(" errno == ERANGE\n"); if(fetestexcept(FE_OVERFLOW)) printf(" FE_OVERFLOW raised\n"); diff --git a/todo b/todo index 00e019c..dc5a6e9 100644 --- a/todo +++ b/todo @@ -48,6 +48,8 @@ stdio.h: * tmpnam * tmpfile_s * tmpnam_s +* Printing 8.98846567431157854073e+307 +* Printing negative zero stdlib.h: * Strtod base 16 must be correctly rounded