Posted 03 October 2012 - 06:52 AM
Hey guys, im here starting with the book and im still at the end of chapter 2.So far so good i should say and the book is really fun and easy to read/learn.
So here i was , trying to play a little with the console projects before proceeding with the next chapter and trying to transfer knowledge from python(which id say im no expert but had my share of coding with it) using simple math programs.
I came to a problem with the math.pow command. In python, you could type x=y**2 and it would work but now in c# is much more dificult(that is my opinion of course)
Here is my bit of code, if any1 can look at it and tell me why it wont return the correct aproximation to the square root of x and instead the answer i get is "0 is close to the square root of 25".
int x = 25;
double epsilon = 0.01;
double step = Math.Pow(epsilon,2);
int numGuess = 0;
double ans = 0.0;
//Now we are going to use a "while"loop first to check if we have gone too far//
while ((Math.Pow(ans,2)-x) >= epsilon && ans <= x)
ans += step;
numGuess += 1; //this generates the new test for the while loop//
Console.WriteLine("numGuess" + numGuess);
if ((Math.Pow(ans, 2) - x) >= epsilon)
Console.WriteLine("failed to square root " + x);
Console.WriteLine(ans + " is close to the square root of " + x);
Any help is fine, just remember im a noobie to C# XD
BTW, if violated the forum rules in any way, please let me know and i will edit my post asap(i couldnt find any post with the rules)