sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit 1bb5a34dfe299c759d4542d9fe2ac816aca14897
parent 74096de403e3fd43bae726c45d9087b47d92f19b
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Thu, 22 Jan 2026 12:09:06 +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.

Diffstat:
Mdc.c | 3++-
Mtests/0010-bc.sh | 2+-
Mtests/0033-dc.sh | 26+++++++++++++-------------
3 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/dc.c b/dc.c @@ -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 @@ -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 @@ -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: