haipart bug



Haipart (x ,n) is some kind of joke for  "high part" and is probably
the smallest integer less than log-base-(2^n) of x.

  according to CL the Language...

The MacLisp function haipart can be implemented in terms of ldb as follows:

(defun haipart (integer count)
   (let ((x (abs integer)))
     (if (minusp count)
         (ldb (byte (- count) 0) x)
         (ldb (byte count (max 0 (- (integer-length x) count)))
              x))))


Raymond Toy wrote:
>>>>>>"Andreas" == Andreas Eder <Andreas.Eder@t-online.de> writes:
>>>>>
> 
>     Andreas> I think the function haipart in clmacs.lisp is buggy.
>     Andreas> Actually there are two definitions in there: the one that is active is
>     Andreas> buggy, and the one that works is commented out because it is
>     Andreas> supposedly slower. But that one is the right one. The currently active
>     Andreas> one gives wrong values for negative count arguments e.g. 
>     Andreas> (haipart 10 -2) gives 0 though it should be 2.
> 
>     Andreas> I enabled the correct one (commented out the bad one) and everything
>     Andreas> seems to be fine. Especially the jacobi symbol in numth.lisp is now
>     Andreas> working correctly.
> 
>     Andreas> This should be corrected in the CVS.
> 
>     Andreas> Just for the record, the correct definition is:
> 
>     Andreas> (defun haipart (integer count)
>     Andreas>   (let ((x (abs integer)))
>     Andreas>     (if (minusp count)
>     Andreas>       (ldb (byte (- count) 0) x)
>     Andreas>       (ldb (byte count (max 0 (- (integer-length x) count))) x))))
> 
> 
> There appears to be a typo in the current haipart.  Would the following
> work better:
> 
> (defun haipart (x n)
>   (let ((x (abs x)))
>     (cond ((< n 0)
> 	   (logand x (1- (ash 1 (- n)))))
> 	  (t
> 	   (ash x (min (- n (integer-length x))
> 		       0))))))
> 
> The bug is (< n 0).  It was (< x 0).
> 
> What does haipart mean anyway?  What is it really supposed to do?
> 
>     Andreas> PS: I think I will go over all the numbertheoretic functions and
>     Andreas> correct them, because most are not working properly (see the comment
>     Andreas> on primep in the mailing list).
> 
> That would be excellent.
> 
>     Andreas> But don't hold yout breath :-)
> 
> It's free software.  Nobody should be holding their breath. :-)
> 
> Ray
> _______________________________________________
> Maxima mailing list
> Maxima@www.math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima