From: Mischa POSLAWSKY Date: Mon, 31 Oct 2022 15:40:35 +0000 (+0100) Subject: shift unit to prevent additional digits after rounding X-Git-Tag: v1.08~8 X-Git-Url: http://git.shiar.nl/barcat.git/commitdiff_plain/a3dd7b92d69d2a964f51b58917611fe25e9b18b7 shift unit to prevent additional digits after rounding Fix 0.9995 from showing up as 1000m instead of 1.0. --- diff --git a/barcat b/barcat index ce3f788..fedb6c9 100755 --- a/barcat +++ b/barcat @@ -112,7 +112,12 @@ $opt{input} = (@ARGV && $ARGV[0] =~ m/\A[-0-9]/) ? \@ARGV : undef $opt{'sum-format'} = sub { sprintf '%.8g', $_[0] }; $opt{'calc-format'} = sub { sprintf '%*.*f', 0, 2, $_[0] }; $opt{'value-format'} = $opt{units} && sub { - my $unit = int(log(abs $_[0] || 1) / log(10) - 3*($_[0] < 1) + 1e-15); + my $unit = int( + log(abs $_[0] || 1) / log(10) + - 3 * ($_[0] < .9995) # shift to smaller unit if below 1 + - log(.9995) / log(10) # 3 digits rounding up + + 1e-15 # float imprecision + ); my $float = $_[0] !~ /^0*[-0-9]{1,3}$/; sprintf('%3.*f%1s', $float && ($unit % 3) == ($unit < 0), # tenths diff --git a/t/t1402-rounded.in b/t/t1402-rounded.in new file mode 100644 index 0000000..edaab9f --- /dev/null +++ b/t/t1402-rounded.in @@ -0,0 +1,24 @@ +999499 - +999500 + +999501 ++ +1000000 +1049999 - +1050000 + + +.000999499999 - +.0009995 + +.001 + +999.49 - +999.50 + +1000 +1049 - +1050 + +9999 + +.999 - +.9994999 - +.9995 + +1.0 +1 +1.000001 diff --git a/t/t1402-rounded_-H_-w1.out b/t/t1402-rounded_-H_-w1.out new file mode 100644 index 0000000..34d6eef --- /dev/null +++ b/t/t1402-rounded_-H_-w1.out @@ -0,0 +1,24 @@ +999k - +1.0M + +1.0M ++ +1.0M +1.0M - +1.1M + + +999μ - +1.0m + +1.0m + +999 - +1.0k + +1.0k +1.0k - +1.1k + + 10k + +999m - +999m - +1.0 + +1.0 + 1 +1.0 diff --git a/t/t1403-precision_-H.out b/t/t1403-precision_-H.out index ee653b1..da5f9ae 100644 --- a/t/t1403-precision_-H.out +++ b/t/t1403-precision_-H.out @@ -8,5 +8,5 @@ 0 160z -150z --1000e-33 +-1.0e-32 -1.5e-32