winzip icon

Fast substitute for the Sqr function [Update]

Submitted on: 5/24/2020 8:27:32 AM
By: Rde 
Level: Intermediate
User Rating: By 1 Users
Compatibility: VB 5.0, VB 6.0
Views: 1757
author picture
     This is a lightning fast substitute for the Sqr floating point function. Sometimes what is needed is the rounded down integer value of a numbers square root, and normally you would perform a full precision floating point calculation with VB's Sqr function, just to the reduce it down to the next closest integer using Int. This is inefficient when we can code a VB function that is around 7 times faster. IntSqr when called in ascending order completely eliminates floating point operations while performing the absolute minimum amount of integer arithmetic. Also included is some experimental code that failed both is precision and speed but is included out of interest. Bug fix update - the exponent could overflow. Changed to Currency.

winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net

Other 25 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

5/22/2020 7:08:48 PMEddie Bole

Hi RDE. Just wondering if your SQR could be used to work out the square of a number, as SQR is the inverse of squaring a number, and if so would it be faster than exponent (^2) or doing number*number? I thought of testing it out when drawing a line and getting it's length using the Pythagoras rule or drawing a triangle. Just a thought. Thanks for you SQR code update. I like looking at and investigating faster (equivalent) algorithms.
(If this comment was disrespectful, please report it.)

5/25/2020 5:38:50 AMRde

Hi Eddie
Thanks for the feedback
Interesting idea, will give it some thought
(If this comment was disrespectful, please report it.)

5/26/2020 4:14:12 AMRde

Hi again

Test: n2 = n * n

0.00012854856556514 seconds
333772 cycles



mov eax,[esp+8]
mul eax
mov edx,[esp+12]
ret 12

0.000280732666368025 seconds
732064 cycles

the overhead of the function call
out-weighs any speed benefits, not
really suitable for simple ops.



shift code as mentioned,
see attached

0.00405369377026836 seconds
10586243 cycles

surprised this out-performs ^2


Test: n2 = n ^ 2

0.0162326904890441 seconds
42394763 cycles

(If this comment was disrespectful, please report it.)


Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.