nth root calculator xcode

Discussion in 'Mac Programming' started by abcdefg12345, Apr 29, 2014.

  1. abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    Anyone know a way to calculate the nth root in Xcode

    I use the following for square root and tube root

    Code:
    - (IBAction)SquareRoot:(id)sender
    {
        float result = sqrt([display floatValue]);
        [display setFloatValue:result];
    }
    Code:
    - (IBAction)QubeRoot:(id)sender
    {
        float result = cbrtf([display floatValue]);
        [display setFloatValue:result];
    }
    i found a way to do it with the powers

    Code:
    - (IBAction)Power:(id)sender
    {
        if ([display2 floatValue]==FALSE)
        {
            float result = [display floatValue];
            [display2 setFloatValue:result];
            (display.stringValue=@"");
        }
            else{
            float result = powf([display2 floatValue], [display floatValue]);
            [display setFloatValue:result];
            (display2.stringValue=@"");
        }
    }
    display2 is a hidden textfield

    anyone know of a way to do roots and also is there a better way to do the powers one
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I don't at all understand how your power: method is working. What should display and display2 each contain before and after the method is run?

    Also, roots are just the inverse of powers. The nth root of x is equal to x^(1/n), where ^ is the power function and not a bitwise XOR. If you have a power function that will accept non-integral powers, use it coupled with the fact I just gave you to write your n-th root function.
     
  3. abcdefg12345 thread starter macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #3
    heres what my powers do

    Code:
    if ([display2 floatValue]==FALSE)
    this checks if the hidden text field is empty or has a value of zero and if it was then it would put the value thats entered into the main textfield into the hidden one and empty the main field using the following code

    Code:
    {
            float result = [display floatValue];
            [display2 setFloatValue:result];
            (display.stringValue=@"");
        }
    
    but if it the hidden textfield has a value and you inter another value into the first field it would power the hidden field to the value in the main field and display it in the main field and erase everything in the hidden field using this code

    Code:
    else{
            float result = powf([display2 floatValue], [display floatValue]);
            [display setFloatValue:result];
            (display2.stringValue=@"");
        }
    so all the use does is enter a number in the main field and click the power button then enters the second value and click the power button again and it would display x^y where x is the hidden text field and y is the main text field

    and thanks for the tip

    i got the roots working the same way

    Code:
    - (IBAction)nthroot:(id)sender
    {
        if ([display2 floatValue]==FALSE)
        {
            float result = [display floatValue];
            [display2 setFloatValue:result];
            (display.stringValue=@"");
        }
            else{
            float result = powf([display2 floatValue], (1/[display floatValue]));
            [display setFloatValue:result];
            (display2.stringValue=@"");
        }
    }
    just had to power the hidden textfield by 1 divided by the first display display2 ^ 1/display

    i compared the results with the mac calculator and got the same so its working for me
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    You really aught to store variables in variables, not hidden text fields...
     
  5. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #5
    What are the hidden textfields for?
     

Share This Page