From: Martin Willi Date: Fri, 27 Sep 2013 14:13:14 +0000 (+0200) Subject: printf-hook-builtin: Correctly round up floating point values X-Git-Tag: 5.1.1rc1~46^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8af9bf70f5d41b10d635974d5723d104f50f3274;p=thirdparty%2Fstrongswan.git printf-hook-builtin: Correctly round up floating point values --- diff --git a/src/libstrongswan/tests/test_printf.c b/src/libstrongswan/tests/test_printf.c index f822e5f4b0..a7b19d96e0 100644 --- a/src/libstrongswan/tests/test_printf.c +++ b/src/libstrongswan/tests/test_printf.c @@ -97,22 +97,42 @@ START_TEST(test_printf_float) verify("1.000000", "%f", 1.0); verify("12345.1", "%.1f", 12345.123); verify("1", "%.0f", 1.0); - verify("1.4", "%.1f", 1.456789); - verify("1.34", "%.2f", 1.3456789); - verify("1.234", "%.3f", 1.23456789); - verify("1.1234", "%.4f", 1.123456789); + verify("1.3", "%.1f", 1.346789); + verify("1.23", "%.2f", 1.23456789); + verify("1.123", "%.3f", 1.123456789); + verify("1.0123", "%.4f", 1.0123456789); verify("-1.000000", "%f", -1.0); verify("-12345.1", "%.1f", -12345.123); verify("-1", "%.0f", -1.0); - verify("-1.4", "%.1f", -1.456789); - verify("-1.34", "%.2f", -1.3456789); - verify("-1.234", "%.3f", -1.23456789); - verify("-1.1234", "%.4f", -1.123456789); + verify("-1.3", "%.1f", -1.3456789); + verify("-1.23", "%.2f", -1.23456789); + verify("-1.123", "%.3f", -1.123456789); + verify("-1.0123", "%.4f", -1.0123456789); verify(" 1.2", "%5.1f", 1.234); verify("001.2", "%05.1f", 1.234); verify("1.2 ", "%-5.1f", 1.234); + + verify("12346", "%.0f", 12345.6789); + verify("2", "%.0f", 1.5); + verify("1", "%.0f", 1.49); + verify("1.2", "%.1f", 1.151); + verify("1.1", "%.1f", 1.149); + verify("1.13", "%.2f", 1.1251); + verify("1.12", "%.2f", 1.1249); + verify("1.124", "%.3f", 1.12351); + verify("1.123", "%.3f", 1.12349); + + verify("-12346", "%.0f", -12345.6789); + verify("-2", "%.0f", -1.51); + verify("-1", "%.0f", -1.49); + verify("-1.2", "%.1f", -1.151); + verify("-1.1", "%.1f", -1.149); + verify("-1.13", "%.2f", -1.1251); + verify("-1.12", "%.2f", -1.1249); + verify("-1.124", "%.3f", -1.12351); + verify("-1.123", "%.3f", -1.12349); } END_TEST diff --git a/src/libstrongswan/utils/printf_hook/printf_hook_builtin.c b/src/libstrongswan/utils/printf_hook/printf_hook_builtin.c index 6ba4841cd2..a28ce7f487 100644 --- a/src/libstrongswan/utils/printf_hook/printf_hook_builtin.c +++ b/src/libstrongswan/utils/printf_hook/printf_hook_builtin.c @@ -500,6 +500,14 @@ static size_t format_double(char *q, size_t n, double val, bpf_flag_t flags, oo = o; tmpval = (uintmax_t)fabs(val); + if (!prec) + { + /* round up if no additional digits */ + if (fabs(val) - tmpval >= 0.5) + { + tmpval++; + } + } while (ndigits > 0) { qq--; @@ -521,9 +529,14 @@ static size_t format_double(char *q, size_t n, double val, bpf_flag_t flags, qq = q; oo = o; + tmpval = (uintmax_t)(fabs(val) * pow(base, prec)); + /* round up if required */ + if (fabs(val) * pow(base, prec) - tmpval >= 0.5) + { + tmpval++; + } while (prec > 0) { - tmpval = (uintmax_t)(fabs(val) * pow(base, prec)); qq--; oo--; prec--; @@ -531,6 +544,7 @@ static size_t format_double(char *q, size_t n, double val, bpf_flag_t flags, { *qq = digits[tmpval % base]; } + tmpval /= base; } }