[Bug 275661] /usr/bin/dc hangs with a trivial calculation

From: <bugzilla-noreply_at_freebsd.org>
Date: Sat, 09 Dec 2023 18:11:42 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=275661

            Bug ID: 275661
           Summary: /usr/bin/dc hangs with a trivial calculation
           Product: Base System
           Version: 13.2-RELEASE
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Many People
          Priority: ---
         Component: standards
          Assignee: standards@FreeBSD.org
          Reporter: dclarke@blastwave.org

While having a discussion about an article written by Martin Gardner
for Scientific American[1] magazine somewhere in the late '70s I was
reminded of the old seven digit calculator problem : 

    enter 1.0000001 if you can
    square that number
    repeat the square 27 times in total
    the result should be 674530.470741 or very close

A great many calculators of the era could not even get close to the
correct result. The Hewlett Packard HP41C and similar were exceptional
in that they compute all intermediate results with greater precision
than is displayed.

To demonstrate the calculation one may use bc :

callisto$ uname -a
FreeBSD callisto 13.2-RELEASE-p4 FreeBSD 13.2-RELEASE-p4 GENERIC amd64
callisto$ freebsd-version -kru
13.2-RELEASE-p4
13.2-RELEASE-p4
13.2-RELEASE-p5

callisto$ bc
scale=16
a=1.0000001
b=a*a
b
1.00000020000001
a=b*b
a
1.0000004000000600
b=a*a
b
1.0000008000002800
a=b*b
a
1.0000016000012000
b=a*a
b
1.0000032000049600
a=b*b
a
1.0000064000201600
b=a*a
b
1.0000128000812802
a=b*b
a
1.0000256003264024
b=a*a
b
1.0000512013081815
a=b*b
a
1.0001024052379369
b=a*a
b
1.0002048209627065
a=b*b
a
1.0004096838770397
b=a*a
b
1.0008195355949585
a=b*b
a
1.0016397428285084
b=a*a
b
1.0032821744135604
a=b*b
a
1.0065751214960018
b=a*a
b
1.0131934752146907
a=b*b
a
1.0265610182176220
b=a*a
b
1.0538275241240008
a=b*b
a
1.1105524506013214
b=a*a
b
1.2333267455366004
a=b*b
a
1.5210948612559022
b=a*a
b
2.3137295769391123
a=b*b
a
5.3533445552028435
b=a*a
b
28.6582979267199303
a=b*b
a
821.2980400566398555
b=a*a
b
674530.4706008780046192
^D
callisto$ 

That is not a bad result given that we were limited to 16 decimal
digits.  The same result *should* be from 1.0000001^( 2 ^ 27 ) :

callisto$ bc
scale=16
2^27
134217728
1.0000001^134217728

^C^C
interrupt (type "quit" to exit)

    0: (main)
ready for more input
^D
callisto$ 


Here we see that the old dc "desk calculator" underneath bc has locked
up in some strange way.


callisto$ which dc
/usr/bin/dc
callisto$ echo '16k  1.0000001  2  27^  p  ^  pq' | dc
134217728
^C^C
    0: (main)
ready for more input
callisto$ 

Just hangs. Never seems to return and I left it alone for quite some
time.  Eventually I hit the CTRL+C and you see the message above.

This seems like strange behavior. Curious about where this behavior
may have come from I went and powered up an old Sun SPARCStation 20.

Yes really ...

s20$ uname -a 
SunOS nix 5.8 Generic_117350-62 sun4m sparc SUNW,SPARCstation-20
s20$ echo '16k  1.0000001  2  27^  p  ^  pq' | dc
134217728
exp too big
empty stack
s20$ 

Similar behavior is seen on an old SGI Indigo running IRIX 5.3 as
well as on an IBM OS400 machine on powerpc.

The calculation can be easily done with logarithms given that the
identity ln( a ^ b ) = b * ln( a ) : 

callisto$ bc -l
scale=16
e( 2^27 * l( 1.0000001 ) )
674530.4707410543814025
^D
callisto$ 

It would be best if dc would not just hang. Seemingly forever.

Dennis Clarke
RISC-V/SPARC/PPC/ARM/CISC
UNIX and Linux spoken

[1] my memory may have failed me but I think it was Martin Gardner

-- 
You are receiving this mail because:
You are the assignee for the bug.