O'Reilly Forums: Help With Basic Math Problem In C# - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help With Basic Math Problem In C# math.pow and transfer knowledge from python

#1 User is offline   Zarzelius 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 03-October 12

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);
}
else
{
Console.WriteLine(ans + " is close to the square root of " + x);
}

Console.ReadLine();


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)
0

#2 User is offline   Sikta_26785 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 67
  • Joined: 18-May 11

Posted 03 October 2012 - 08:54 PM

View PostZarzelius, on 03 October 2012 - 06:52 AM, said:


while ((Math.Pow(ans,2)-x) >= epsilon && ans <= x)



It never loops. 0^2 = 0. 0-25 is -25. -25 is not greater than or equal to epsilon so it doesn't loop and fails the if statement so it prints out that 0 is close to the square root of 25.
0

#3 User is offline   Zarzelius 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 03-October 12

Posted 04 October 2012 - 09:47 AM

View PostSikta_26785, on 03 October 2012 - 08:54 PM, said:

It never loops. 0^2 = 0. 0-25 is -25. -25 is not greater than or equal to epsilon so it doesn't loop and fails the if statement so it prints out that 0 is close to the square root of 25.


Ok , thanx for the answer.I will take a look at it and change it so it does what is meant to XD
Ill post back if i have another question.Again, thanx a lot for the help
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users