Wanted: Efficient Algorithm to Square an Integer Number
03-07-2019, 02:52 PM (This post was last modified: 03-08-2019 12:21 PM by Martin Hepperle.)
Post: #7
 Martin Hepperle Senior Member Posts: 411 Joined: May 2014
RE: Wanted: Efficient Algorithm to Square an Integer Number
(03-06-2019 08:10 PM)ijabbott Wrote:  I'm not aware of clever tricks for squaring an integer (and most modern CPUs have fast multiply instructions), but this seems like another opportunity to recommend the book Hacker's Delight (2nd Edition) by Henry S. Warren, Jr.

Thank you, this is very interesting and surely comes in handy for future coding problems.

My CPUs of interest are older and do not offer multiplication (e.g. 6800 and HP-Series-80 Capricorn).

I finally found this explanation for the existance of the Sqr() function in "FreePascal from Square One" (http://www.copperwood.com/pub/FreePascal...areOne.pdf):

"11.2. Sqr and Sqrt
Nothing complicated here. Sqr(X) squares X. It is completely equivalent to X * X,
and Pascal includes it because squaring is done so frequently in mathematics, and
also because (as we will discuss later) there is no exponentiation operator in Standard
Pascal and hence no clean notation for X raised to a power of two. FreePascal does
have an exponentiation operator, as I’ll explain shortly, and so with FreePascal, Sqr
is unnecessary."

So the Sqr() function in Pascal seems to be unrelated to speed, contrary to what I had thought.

Thank you all for your help,
Martin
 « Next Oldest | Next Newest »

 Messages In This Thread Wanted: Efficient Algorithm to Square an Integer Number - Martin Hepperle - 03-06-2019, 03:54 PM RE: Wanted: Efficient Algorithm to Square an Integer Number - ijabbott - 03-06-2019, 08:10 PM RE: Wanted: Efficient Algorithm to Square an Integer Number - Martin Hepperle - 03-07-2019 02:52 PM RE: Wanted: Efficient Algorithm to Square an Integer Number - David Hayden - 03-06-2019, 08:16 PM RE: Wanted: Efficient Algorithm to Square an Integer Number - Thomas Okken - 03-06-2019, 11:24 PM RE: Wanted: Efficient Algorithm to Square an Integer Number - Jim Horn - 03-08-2019, 12:26 AM RE: Wanted: Efficient Algorithm to Square an Integer Number - Gerson W. Barbosa - 03-06-2019, 11:47 PM RE: Wanted: Efficient Algorithm to Square an Integer Number - Druzyek - 03-07-2019, 12:10 AM

User(s) browsing this thread: 1 Guest(s)