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 15 Junior Poster

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

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Black Magic 15 Junior Poster

Thanks but i think i'll leave that option out

VernonDozier 2,218 Posting Expert Featured Poster

Sky Diploma 571 Practically a Posting Shark

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

Black Magic 15 Junior Poster

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

sarehu 84 Posting Whiz in Training

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

hammerhead 19 Posting Whiz in Training

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()?

Black Magic 15 Junior Poster

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

hammerhead 19 Posting Whiz in Training

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.