shift unit to prevent additional digits after rounding
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 31 Oct 2022 15:40:35 +0000 (16:40 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 1 Nov 2022 02:14:50 +0000 (03:14 +0100)
Fix 0.9995 from showing up as 1000m instead of 1.0.

barcat
t/t1402-rounded.in [new file with mode: 0644]
t/t1402-rounded_-H_-w1.out [new file with mode: 0644]
t/t1403-precision_-H.out

diff --git a/barcat b/barcat
index ce3f788ec111f137efb183f86ae3a9886cd3b79a..fedb6c9081334a981f796a40d3201ea57f4b7170 100755 (executable)
--- 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 (file)
index 0000000..edaab9f
--- /dev/null
@@ -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 (file)
index 0000000..34d6eef
--- /dev/null
@@ -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     
index ee653b10b606a38734b0761dea994705ced120c9..da5f9aeca1115774febd77c8f50c5fa588775134 100644 (file)
@@ -8,5 +8,5 @@
   0  
 160z 
 -150z 
--1000e-33 
+-1.0e-32 
 -1.5e-32