public float xroot(float radicand, float root)
{
float final;
float trynum;
float orignum;
//number to tell how far to round
float smallnum = 0.0000001f;
orignum = radicand / 4;
trynum = orignum;
for (int i = 0; i <= root - 1; i++)
{
trynum *= trynum;
}
while (Math.Abs(trynum - radicand) > smallnum && trynum > radicand)
{
orignum *= 0.999f;
trynum = orignum;
for (int i = 0; i <= root - 1; i++)
{
trynum *= trynum;
}
}
while (Math.Abs(trynum - radicand) > smallnum && trynum < radicand)
{
orignum *= 1.0001f;
trynum = orignum;
for (int i = 0; i <= root - 1; i++)
{
trynum *= trynum;
}
}
final = trynum;
return final;
}
The problem is that when I run it with relatively small numbers as the root, it'll add some small number to the radicand and return that as the answer.
For example, if I do this: 2x√6 I'll get 6.000726 as my answer. Whereas if I have a number like ten as the root, it'll just put out "Infinity".
I've checked over and can't find anything APPARENTLY wrong with it. Also, please don't make comments on how my code isn't very efficient or whatever, I'm gonna make it be more efficient in the future. Anyway, if anyone could just point out where I made a mistake it'd be greatly appreciated.

New Topic/Question
Reply




MultiQuote







|