Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » How to express the cubic root of a variable?
Closed Thread
Old 09-22-2006, 12:19 PM   #1 (permalink)
 
Junior Techie

Join Date: Oct 2004

Posts: 60

Fabyfakid

Default How to express the cubic root of a variable?

Need help; trying to get the cubic root of a variable which I have declared as a defined integer, and getting a value of 0. What could I be doing wrong?

doing this:

int x = "number I want";
pow(x, (1/3));


Thanks in advanced for your help
Fabyfakid is offline  
Old 09-22-2006, 01:09 PM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

What language are you using? Try 1.0 instead of just 1 to force the result of the division to be a floating point number. For instance, in C#
Code:
int x = 8;  // find the cube root of x, which should be 2

// This returns the wrong result because it uses integer division
Math.Pow(x, 1/3);

// This returns the correct result because it uses floating point division
Math.Pow(x, 1.0/3);

jaeusm is offline  
Old 09-22-2006, 03:31 PM   #3 (permalink)
 
Junior Techie

Join Date: Oct 2004

Posts: 60

Fabyfakid

Default

Sorry for not mentioning it, but I'm using C.
Fabyfakid is offline  
Old 09-22-2006, 03:49 PM   #4 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

My suggestion still works for C. Try this:

Code:
#include <math.h>
#include <stdio.h>

int main(){
    int x = 8;
    double y = -1;

    y = pow(x, 1.0/3);
    printf("%f", y);
    
    return 0;
}

jaeusm is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On