Post Reply 
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
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Wanted: Efficient Algorithm to Square an Integer Number - Martin Hepperle - 03-07-2019 02:52 PM



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