[comp.bugs.4bsd] bc library bug +Fix

angst%csilvax@hub.ucsb.edu (vaguely human) (04/24/88)

This is my first time posting to this newsgroup, so please forgive me if
I seem incompetent (those who know me can attest to this incompetence).

There is a bug in the exponentiation routine in the bc library.  It uses
a variable called t, but does not declare t, resulting in the global t
being altered.  This is quite annoying if you routinely write functions
using t as an argument, as I do.

To repeat, use this file as input to bc -l:

    define r(l, t) {
	    auto x, y

	    l
	    t
	    x = 3.0 * e(-2*l*t)
	    l
	    t
	    y = 2.0 * e(-3*l*t)

	    return (x - y);
    }

    r(0.01, 0.0)

The output is:

    .01
    0
    .01
    20
    1.90237672781194713474

when it clearly should be:

    .01
    0
    .01
    0
    1.0

The value of t is changed from 0 to 20 after the computation of x.  This
occurs as a result of the call to the function e.  Inspection of the
function definition in /usr/lib/lib.b shows that t is used to hold the
current value of scale, so it can be changed in the function.  It is not,
however, declared as a local variable, which it should be --

    /*	lib.b	4.1	83/04/02	*/

    scale = 20
    define e(x){
	    auto a, b, c, d, e, g, w, y

	    t = scale
	    scale = t + .434*x + 1

	    w = 0
	    ...
			    scale = t
			    if(w==1) return(1/g)
			    return(g/1)
		    }
		    e=g
	    }
    }

To fix it, simply change the declaration line to add t, as follows:

	    auto a, b, c, d, e, g, t, w, y

Happy exponentiating!

	     "The stuff that undermines the best of me and you"

It was a pleasure |	        Dave Stein	      | on my back.  Just a
to meet you, you  |     angst%csilvax@hub.ucsb.edu    | pleasure to meet you,
slapped me right  |  ...ucbvax!ucsbcsl!csilvax!angst  | you got it almost exact.