[comp.bugs.4bsd] Bug in bc square root routine

pmontgom@sdcrdcf.UUCP (Peter Montgomery) (04/27/87)

	The following bc program prints (comments added by me)

999999996999999999700000000001          (b = actual square root)
999999994000000008400000001802000000083999999999400000000001   (c = square)
999999996999999999699950000000          (d = computed square root)
50000001                                (b-d = difference)

        The problem exists both on a VAX 780 using 4.2bsd and on a SUN
2/170 using SUN release 3.0.

a = 10^10
b = 1 - 30*a - 30*a^2 + a^3
c = b^2
d = sqrt(c)
b
c
d
b-d
quit
-- 
	Peter Montgomery     {aero,allegra,bmcg,burdvax,hplabs,ihnp4,
			      psivax,randvax,sdcsvax,trwrb}!sdcrdcf!pmontgom

Don't blame me for the crowded freeways - I don't drive.

mike@arizona.edu (Mike Coffin) (04/27/87)

In article <4534@sdcrdcf.UUCP>, pmontgom@sdcrdcf.UUCP (Peter Montgomery)
gives an example of a bc program that produces an incorrect square root.
I do not know if this is related, but there is also bug in the division
routine of bc, at least on VAX running Berkley 4.2 and 4.3.  I had
several examples of this once; this is the only one I can find at the
moment:
-------------------------------------------------------------------------
% bc
obase=16
ibase=16
a = 519E2FEA51A537AB0132DC6F3678E8539F1BEF64109473F752
b = A2F01268A40089
a
519E2FEA51A537AB0132DC6F3678E8539F1BEF64109473F752
b
A2F01268A40089
c = a/b
c
803BF0FC7753CC1552D0A09B286CAAEBE771
a-(b*c)
F530645557A101E4C95E9095BD9
obase=A
c
11170769584511689511742018590-12440481240754033
---------------------------------------------------------------------------
The variable c in the above transcript is obviously bogus; a mod b
cannot be larger than b.  And, when printed in base 10, it contains a
minus-sign.  BTW, the correct answer for c is
803BF0FC7753CC1552D0A0CE7125CE93A4C4 (base 16).

						mike coffin
						(mike@arizona.edu)