Jump to content

Square root or multiplication

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
echoBravo

echoBravo

    Newbie

  • Members
  • Pip
  • 3 posts
I have an equation (in C++) which looks something like this in pseudo code:

sqrt(x^2 + y^2) = r

I have read somewhere, that it is computationally easier to re-write the equation so there is only multiplications and no square root, like this:

x^2 + y^2 = r^2

Can anybody verify that the latter is the way to go in terms of optimized, fast code? And can you show me some reliable documentation that confirms it? I need the documentation for a report I am writing. Thanks!

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
Well, you can't calculate that way in C/C++, you need to do it the assign style, not the equation style...

and if you're writing a scientific report, an answer from a forum isn't valid, you would need to do references from publicated papers...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
echoBravo

echoBravo

    Newbie

  • Members
  • Pip
  • 3 posts

Orjan said:

Well, you can't calculate that way in C/C++, you need to do it the assign style, not the equation style...

I am aware how to do the calculations in C/C++, and that was not my question. I merely provided some pseudo code for you to understand my problem.

Orjan said:

and if you're writing a scientific report, an answer from a forum isn't valid, you would need to do references from publicated papers...

I am aware of this, and that is exactly why I am asking if anyone knows about some reliable documentation.

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
so your real question is if x^2 is faster than sqrt(x) ?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
echoBravo

echoBravo

    Newbie

  • Members
  • Pip
  • 3 posts

Orjan said:

so your real question is if x^2 is faster than sqrt(x) ?

I just realised, that I missed a "=", so my original equation should be sqrt(x^2 + y^2) == r, that is, I want to compare the left side to the right side (a boolean expression).

My questions are: is it faster to square both sides of the expression sqrt(x^2 + y^2) == r (to avoid the square root) before doing the comparsion? If so, why is it faster? And is there any reliable documentation that verifies this?

Sorry for being imprecise! :(

Actually, I've made a small C++ program that tests it, and it seems that the last version (x^2 + y^2 == r^2) is faster, so now I want to know why? :)

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
sqrt() requires significantly more processing power to achieve than just squaring a value. Remember that squaring is nothing more than multiplying a number by itself, whereas sqrt() is a difficult proposition. What's the square root of 2? Your algorithm has to be prepared to come up with an answer for that, and the answer happens to be an irrational number so it's not even possible to represent that as a finite sequence of bits. While I don't know how sqrt() is actually implemented (it's probably different with each implementation of the C standard library), I do know it's far more complex than simply multiplying two numbers. :)

EDIT: Remember that there IS a limitation to your algorithm! Your algorithm to determine if a triangle is a right triangle (that's why you're comparing a^2 + b^2 == c^2, right?) is limited in size for a and b, because the resultant value from their squaring and subsequent addition could very easily overflow the value of the temporary values the processor would actually compare. For example, if you had int a = 33,000 and int b = 33,000, when each of them are squared they will equal 1,089,000,000. Then, when they're added together, (1,089,000,000 x 2 = 2,178,000,000) it will overflow the maximum size of an integer (which happens to be 2,147,483,647), and then even if the triangle were a right triangle it'd still return false. I'd cast one of those integers into a long, which will then do it right.
Wow I changed my sig!

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
my guess is that a square is pretty simple as it's just to multiply a number with itself. it's probably demanding more steps for a processor to reverse it, I don't know how to do that without trying to reach the result...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Squaring requires a simple multiplication. Most square root algorithms involve multiple divisions to successively approximate the the solution.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog