954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Square root??

Hey i've made a simple calculator, but i was thinking about adding a square root option..

But is there a actual way to do this or would i have to create my own??

PLEASE REPLY A.S.A.P

Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 

Read this

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

Thanks but i think i'll leave that option out

Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

You can try to bruteforce it out and just increment by 0.01 maybe that will work out.
But the answer wouldnt be exact.
So u can try in for an approx val.

Then test it up. If the value you got. is squared is lesser than the main number of which you should find the square root then increment. But if the the square of the value increases take the previous number as the approximate square root. So you will only be missing the minutest details

Sky Diploma
Practically a Posting Shark
864 posts since Mar 2008
Reputation Points: 673
Solved Threads: 130
 

I've looked at websites etc, but tbh is it worth it?

Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 

I'm confused, is the sqrt function somehow deficient?

sarehu
Posting Whiz in Training
289 posts since Oct 2007
Reputation Points: 98
Solved Threads: 22
 

A simple method for calculating square root of a number "a" is given by newton iteration as

for(int i=0;i<10;i++)
    {
    xn=.5*(x0+(a/x0));
    x0=xn;
    }


where x0 is initialized to 1. Use float comparisons and loop till xn equals x0 here I have used a loop for simplicity. Again why cant you use sqrt()?

hammerhead
Posting Whiz in Training
257 posts since May 2006
Reputation Points: 46
Solved Threads: 24
 

I never said i cant use sqrt() i just said i dont know if it's worth it

Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 

A simple method for calculating square root of a number "a" is given by newton iteration as

for(int i=0;i<10;i++)
    {
    xn=.5*(x0+(a/x0));
    x0=xn;
    }

where x0 is initialized to 1. Use float comparisons and loop till xn equals x0 here I have used a loop for simplicity. Again why cant you use sqrt()?

Sorry I forgot to mention that xn will give you the square root. You can initialize to any other number also.

hammerhead
Posting Whiz in Training
257 posts since May 2006
Reputation Points: 46
Solved Threads: 24
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You