jin Otanashi

Ranch Hand

Posts: 123

Tushar Goel

Ranch Hand

Posts: 934

4

Campbell Ritchie

Marshal

Posts: 55678

161

jin Otanashi

Ranch Hand

Posts: 123

posted 2 years ago

I know i can use Int but the problem is i'm using approximation of the square root that why im using double. by they way if i were to use Int then i won't need this calculation

what i trying to do is like a loop of calculation that output each calculation that the program had done instead of just give me the result or answer without showing the calculation. I think the calculation would look like a right triangle

Campbell Ritchie wrote:You have written that loop wrongly; you can remove about ⅝ of it.Why are you asking for doubles in your square root method and taking ints from the keyboard? I would suggest you change the loop todo…while (num >= 0);

Don't start method names with CapitalLetters.

I know i can use Int but the problem is i'm using approximation of the square root that why im using double. by they way if i were to use Int then i won't need this calculation

what i trying to do is like a loop of calculation that output each calculation that the program had done instead of just give me the result or answer without showing the calculation. I think the calculation would look like a right triangle

jin Otanashi

Ranch Hand

Posts: 123

Campbell Ritchie

Marshal

Posts: 55678

161

posted 2 years ago

Don't know. It is such a long time since I saw that algorithm that I have forgotten how to use it. Is that Newton's Approximation? It is at least 45 years since I last saw it. Read about it on Wikipedia or here or here or here or lots of other links

I meant, if your square root method takes a double, why are you getting an int from the keyboard? Why not use nextDouble?

I meant, if your square root method takes a double, why are you getting an int from the keyboard? Why not use nextDouble?

posted 2 years ago

You need to have an algo that converges to your approximation, the formula you have does not.

I did a quick and dirty little algo and this worked for me if I don't be too crazy with it--just put the target number in for the guess and it brings thing around to a good approximation:

dTarget is the number you want the squareroot

dTrial is the guess (put dTarget in here for a start)

dTolerance is the error you are willing to accept (0.0001 seems to work well)

jin Otanashi wrote:Les Morgan wrote:You're approximation algorithm is flawed:

(N*N)/(2N) -> infinity. Your method is divergent.

you mean i need to do like (high * low )/2 ?

You need to have an algo that converges to your approximation, the formula you have does not.

I did a quick and dirty little algo and this worked for me if I don't be too crazy with it--just put the target number in for the guess and it brings thing around to a good approximation:

dTarget is the number you want the squareroot

dTrial is the guess (put dTarget in here for a start)

dTolerance is the error you are willing to accept (0.0001 seems to work well)

Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH

jin Otanashi

Ranch Hand

Posts: 123

posted 2 years ago

i see thank you for the correction, but somehow the calculation isnt right and return isnt giving my the result if i want sqrt of 25 just give me 1.0 same if i wants to use a number that isnt in the square root like 12 still giving the result of 1.0, i guess this code is wrong, i guess i will go back to the old code

Les Morgan wrote:jin Otanashi wrote:Les Morgan wrote:You're approximation algorithm is flawed:

(N*N)/(2N) -> infinity. Your method is divergent.

you mean i need to do like (high * low )/2 ?

You need to have an algo that converges to your approximation, the formula you have does not.

I did a quick and dirty little algo and this worked for me if I don't be too crazy with it--just put the target number in for the guess and it brings thing around to a good approximation:

dTarget is the number you want the squareroot

dTrial is the guess (put dTarget in here for a start)

dTolerance is the error you are willing to accept (0.0001 seems to work well)

i see thank you for the correction, but somehow the calculation isnt right and return isnt giving my the result if i want sqrt of 25 just give me 1.0 same if i wants to use a number that isnt in the square root like 12 still giving the result of 1.0, i guess this code is wrong, i guess i will go back to the old code

jin Otanashi

Ranch Hand

Posts: 123

jin Otanashi

Ranch Hand

Posts: 123

Campbell Ritchie

Marshal

Posts: 55678

161