Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
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
 
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.
 
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.

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

You really aught to store variables in variables, not hidden text fields...
 
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

What are the hidden textfields for?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.