[hackers] [sbase] dc: Use scale for number of digits in multiplication || Roberto E. Vargas Caballero
commit 1bb5a34dfe299c759d4542d9fe2ac816aca14897
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.net>
AuthorDate: Thu Jan 22 12:09:06 2026 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.net>
CommitDate: Thu Jan 22 12:40:34 2026 +0100
dc: Use scale for number of digits in multiplication
The scale factor has to be considered in the multiplication because
many algorithms will depend of the precision configured in the scale,
that, until now, was considered only for division. BSD and Plan9
dc differ about how to handle this, and plan9 always use only the
scale factor, while BSD uses the maximun of the scale factor or the
bigger scale of the operands. The second seems more sensible and
produces output that user would expect, for example 0k 0.5 0.5*p would
produce 0.2 with the BSD criteria, but it would generate 0 with the
plan9 criteria.
diff --git a/dc.c b/dc.c
index f7e809c..e7b6989 100644
--- a/dc.c
+++ b/dc.c
_AT_@ -664,7 +664,8 @@ mulnum(Num *a, Num *b)
}
norm(c);
- /* Adjust scale to max(a->scale, b->scale) while c is still positive */
+ if (sc < scale)
+ sc = scale;
sc = c->scale - sc;
if (sc > 0) {
div10(c, sc);
diff --git a/tests/0010-bc.sh b/tests/0010-bc.sh
index 43198a1..e7ea350 100755
--- a/tests/0010-bc.sh
+++ b/tests/0010-bc.sh
_AT_@ -10,7 +10,7 @@ a(0)=0
a(1)=.78539816339744830961
a(2)=1.10714871779409050301
a(0.9)=.73281510178650659163
-a(-0.9)-.73281510178650659163
+a(-0.9)=-.73281510178650659163
s(0)=0
s(a(1))=.70710678118654752439
s(4*a(1))=.00000000000000000002
diff --git a/tests/0033-dc.sh b/tests/0033-dc.sh
index aab5be8..0fc7184 100755
--- a/tests/0033-dc.sh
+++ b/tests/0033-dc.sh
_AT_@ -61,31 +61,31 @@ test 25:
test 26:
.0625000000
test 27:
-2.25
+2.2500
test 28:
-3.375
+3.375000000
test 29:
-.25
+.2500
test 30:
-.125
+.125000000
test 31:
-2.25
+2.2500
test 32:
--3.375
+-3.375000000
test 33:
-1.5625
+1.56250000
test 34:
-.0625
+.06250000
test 35:
-.015625
+.0156250000
test 36:
-.0625
+.06250000
test 37:
--.015625
+-.0156250000
test 38:
-.015625
+.0156250000
test 39:
--.001953125
+-.0019531250
test 40:
4.0000000000
test 41:
Received on Thu Jan 22 2026 - 12:41:14 CET
This archive was generated by hypermail 2.3.0
: Thu Jan 22 2026 - 12:48:32 CET