Results 1 to 16 of 16

Thread: Cube Root Error

Threaded View

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Cube Root Error

    Much to my disappointment I recently discovered that VB can not calculate the cube root (or any odd root) of a negative number even though it is a real answer.

    For instance if you try
    Code:
    MsgBox((-1)^(1/3))
    You should get -1 because -1*-1*-1 = -1. But no, VB will return NaN, not a number.

    Anyways, my problem is that I'm making a graph program and the user needs to be allowed to enter any formula they want and have it evaluated so the program can graph it. But if the user enters anything with x^(1/3) or like it, it will fail.

    So I was wondering if anyone knows of a way around this problem, such as an extension or patch or something that will fix the issue in Visual Basic. I haven't tried VB 2010 but does anyone know if they have they fixed it yet in that version?

    Also I can't just use Math.Abs because like I mentioned, the user could enter anything, so if they put something like this:

    (x - 250)^(1/3) + (x)^(1/x)

    It would be way to complex to evaluate everything in the parenthesis, check for the negatives that are being squared/rooted, take the absolute value, then finish the square, and then restore the sign.

    If there was an easier way to evaluate expressions in VB then maybe I could do something with parsing but as of now my method is by compiling the formula (string) the user entires as VB code and letting it solve itself by replacing x with the number I need for the coordinate.
    Last edited by Vectris; Feb 27th, 2010 at 11:37 PM.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width