11 Replies - 417 Views - Last Post: 07 February 2012 - 03:45 PM Rate Topic: -----

Topic Sponsor:

#1 superkb10  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 230
  • Joined: 27-November 11

Completely baffled with this one

Posted 06 February 2012 - 07:27 PM

Hey all, I've been writing a windows form calculator, and I have this piece of code that will take a number and find the x root of that number. Here's the code, I'm sorry it doesn't have too many comments in it, I'm not great at doing that.


       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.

Is This A Good Question/Topic? 0
  • +

Replies To: Completely baffled with this one

#2 LaughingBelly  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 88
  • Joined: 11-April 11

Re: Completely baffled with this one

Posted 06 February 2012 - 08:05 PM

You want orignum to be returned as final. trynum is supposed to be equal or close to radicand. This is the reason you see 6.000726. at that point orignum has the xroot you are looking for.
Was This Post Helpful? 0
  • +
  • -

#3 superkb10  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 230
  • Joined: 27-November 11

Re: Completely baffled with this one

Posted 06 February 2012 - 08:30 PM

Oh, right! :stupid:

But it still isn't working quite right, I'm still getting strange values.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 535
  • View blog
  • Posts: 1,506
  • Joined: 04-October 09

Re: Completely baffled with this one

Posted 06 February 2012 - 08:33 PM

Part of your problem is lines 14-19. You aren't raising trynum to the power of root like I believe you think you are. Let's see what we'd get if orignum is 2 and root is 4 (which should give us 16)

trynum = 2
i = 0 and is less than root - 1 (3)
trynum *= trynum = 4
i = 1 and is less than root - 1 (3)
trynum *= trynum = 16
i = 2 and is less than root - 1 (3)
trynum *= trynum = 256
i = 3 and is not less than root - 1 (3) so we are done

Do you see the problem? Do you see how to fix it (Hint: orignum). You do the same thing in lines 24-29
Was This Post Helpful? 0
  • +
  • -

#5 RexGrammer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 152
  • View blog
  • Posts: 664
  • Joined: 27-October 11

Re: Completely baffled with this one

Posted 07 February 2012 - 11:25 AM

Do you know there is a Math.Sqrt()? Just checking.
Was This Post Helpful? 0
  • +
  • -

#6 superkb10  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 230
  • Joined: 27-November 11

Re: Completely baffled with this one

Posted 07 February 2012 - 02:04 PM

RexGrammer Yes, I know there is a Math.Sqrt(), but I want this not to just be to the root of 2 (square root), but to the root of any number. And Momerth, thanks for the help, I see it now but I've found there's a Math.Pow() and I think that'll just be simpler. But thanks for all the help!
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1451
  • View blog
  • Posts: 5,763
  • Joined: 21-March 08

Re: Completely baffled with this one

Posted 07 February 2012 - 02:11 PM

Something like this should work fine for any root...

private double CalculateRoot(double value, int root)
{
    return Math.Pow(value, (1.0 / root));
}


Was This Post Helpful? 0
  • +
  • -

#8 superkb10  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 230
  • Joined: 27-November 11

Re: Completely baffled with this one

Posted 07 February 2012 - 02:25 PM

Yeah, that's what I ended up doing.
Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 535
  • View blog
  • Posts: 1,506
  • Joined: 04-October 09

Re: Completely baffled with this one

Posted 07 February 2012 - 02:35 PM

And here I thought you were coding your own root function for the knowledge. Silly me.
Was This Post Helpful? 1
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • Coder-ian
  • member icon

Reputation: 1401
  • View blog
  • Posts: 7,358
  • Joined: 29-May 08

Re: Completely baffled with this one

Posted 07 February 2012 - 02:48 PM

I hope you added a check for zeroth root.
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1451
  • View blog
  • Posts: 5,763
  • Joined: 21-March 08

Re: Completely baffled with this one

Posted 07 February 2012 - 02:58 PM

View PostAdamSpeight2008, on 07 February 2012 - 05:48 PM, said:

I hope you added a check for zeroth root.


And negative numbers(both the value and root).
Was This Post Helpful? 0
  • +
  • -

#12 superkb10  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 230
  • Joined: 27-November 11

Re: Completely baffled with this one

Posted 07 February 2012 - 03:45 PM

Momerath, no, I've been trying to just get a better understanding of Windows Forms, I've been programming mainly in Console Applications. Although I may come back to the roots later.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1