Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

How to you cube a number

Options
  • 29-06-2007 12:05pm
    #1
    Registered Users Posts: 11,038 ✭✭✭✭


    Kind of embarrassed asking this question, but how do you cube a double? as in i have the following calculation:
    answer = double1 * (double2 / double3) ^3
    

    there is a quick way to do this, right?


Comments

  • Registered Users Posts: 2,345 ✭✭✭ErinGoBrath


    x = x * x * x

    ?


  • Registered Users Posts: 273 ✭✭stipey


    it would help if you told people the development language you are using.

    Most modern languages should have a number of build in librarys (sp?) that will do this for you.

    If there is not, why not write a function that accepts the number to be raised to a certain power and also the power to raise it to. Then you can use a loop to perform the calculations and return it.

    This is just of the top of my head... there is probably a more sound, efficient manner to perform the calculation without using a loop (through recurrsion for example) but I'm not going to do all of the work for you. :p


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Obviously to cube a single number (let's call it x1), then that's just x1^3.

    Given the calculation that you've posted, I'm assuming you have three values. We'll call them x, y and z. You want to divide y by z, multiply the answer by x and then cube that answer?

    As stipey says, what language? There are shorthands for these kinds of things, depending on language.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Even if there isn't a shorthand...

    Perform the multiplication/division as needed. Store the result in an interim variable. Cube this variable either using shorthand or (as pointed out) multiply it by itself and then by itself again.

    Done.


  • Registered Users Posts: 6,790 ✭✭✭cornbb


    dulpit wrote:
    answer = double1 * (double2 / double3) ^3
    

    Instead of the above, try this:
    interimAnswer = double1 * (double2 / double3)
    answer = interimAnswer * interimAnswer * interimAnswer
    

    Should work regardless of language/environment, but may not necessarily be the most efficient way of doing things if your program needs to be optimised.

    Edit: er yup, exactly what bonkey said :o


  • Advertisement
  • Registered Users Posts: 273 ✭✭stipey


    First of all, any of the solutions already posted can be used to cube the result of your original calculation.

    As its a slow enough day, and my project is ahead of schedule, I had a quick look at writing a recursive function that will calculate any double value to the power of n (where n is a positive integer). So here it is in Java...
    	public static double Power(double number, int power)
    	{
    	    // Any number to the power of 0 is 1....
    	    if (power == 0)
    	        return 1;
                
                // Any number to the power of 1 is the number itself....	    
    	    else if (power == 1)
    	        return number;
                
                // Otherwise, call the function recursively
    	    else
    	     return number * Power(number, power - 1);
    	}
    

    Ideally you would include an if statement to check if the power is less than 0 and then either return something like -1 or throw an exception.

    In your case, if you coded something similar in your programming language of choice you could call it as....
    double interimAnswer = double1 * (double2 / double3);
    double result = Power(interimAnswer, 3);
    
    .. or...
    
    double result = Power(double1 * (double2 / double3), 3);
    


  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    This sort of thing should probably be left as a "go google it", but in the interests of learning, most languages (that I've seen anyway) don't have a specific function or method to cube a number - instead, there's the obvious do-it-longhand method, or there's a function to raise a number to an arbitrary power (seeing as people are using java here, that'd be Math.pow() in java.lang.Math, I think? I don't write Java, so I'm not entirely au fait), or there's an operator for this same job (typically ^ or **).

    This will be covered in any language text book for whatever you're writing in, I'm pretty sure?
    Gadget


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,079 Mod ✭✭✭✭AlmightyCushion


    stipey wrote:
    Ideally you would include an if statement to check if the power is less than 0 and then either return something like -1 or throw an exception.

    You can cube a negative number.


  • Closed Accounts Posts: 5,857 ✭✭✭professore


    PHP:

    number pow ( number $base, number $exp )
    Returns base raised to the power of exp.

    C++ would be x ^ 3

    or if you want to roll your own:

    double power(double x,int power)
    {
    if(power == 0)
    return 1;

    for(int i=0;i<power;i++)
    {
    x *= x;
    }

    return x;
    }

    Bob


  • Registered Users Posts: 273 ✭✭stipey


    This sort of thing should probably be left as a "go google it", but in the interests of learning, most languages (that I've seen anyway) don't have a specific function or method to cube a number - instead, there's the obvious do-it-longhand method, or there's a function to raise a number to an arbitrary power (seeing as people are using java here, that'd be Math.pow() in java.lang.Math, I think? I don't write Java, so I'm not entirely au fait), or there's an operator for this same job (typically ^ or **).

    Obviously implementing my solution this in a Java project would be overkill, given the wealth of system classes available (including Math.pow() as you mentioned). However we don't know if this functionality is available to the OP via some handy in-built method so, partly through idleness, I threw together a function that will meet the OPs needs and be more flexible (and reusable) than n * n * n.

    If needed the OP can code something similar in their chosen language - I just used Java because I had Eclipse open and I wanted to make sure the code actually ran before posting it.

    I agree though - this is a question that has probably been asked thousands of times on forums all over the place.


  • Advertisement
  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    You can cube a negative number.

    I think you (both) have missed the point.

    stipey never said you couldn't cube a negative number - he was referring to raising a number to a negative power. But after a quick look, python seems to disagree:
    In [1]: 2**-1
    Out[1]: 0.5
    
    In [2]: 2**-2
    Out[2]: 0.25
    
    In [3]: 2**2
    Out[3]: 4
    

    It would appear that m^n {where n < 0} == 1/(m^abs(n))?

    Gadget


  • Closed Accounts Posts: 97 ✭✭koloughlin


    You can cube a negative number.

    You are correct, but that's not what stipey was saying. He was saying to check that the power, not the the number to be raised to a power. In his code if the power was negative it would have ended up in a never ending loop.

    If the power is negative it could be handled by making it positive and then returning 1 over the final result.

    x^-y = 1/(x^y)

    Beat me by 60 seconds Gadget :)


  • Registered Users Posts: 273 ✭✭stipey


    You can cube a negative number.

    When I said "you would include an if statement to check if the power is less than 0" I meant the power argument that is passed into the method.
    public static double Power(double number, int power)
    

    Maybe i should have specified that i was referring to the power argument specifically.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,079 Mod ✭✭✭✭AlmightyCushion


    stipey wrote:
    When I said "you would include an if statement to check if the power is less than 0" I meant the power argument that is passed into the method.

    :o Oopsy, my bad.


  • Registered Users Posts: 273 ✭✭stipey


    Man, you got it from all sides there - no offence meant, nothing personal and all that.

    Well not from me anyway - the other lads might have had ulterior motives :P


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    Still you can raise a number to a negative value so there is no need to check if it's below 0. Actually stipey's Power function is possibly the most pointless code ever written since Math.pow() will do it for you and it's what he is using in the end anyway :D


  • Registered Users Posts: 273 ✭✭stipey


    Still you can raise a number to a negative value so there is no need to check if it's below 0. Actually stipey's Power function is possibly the most pointless code ever written since Math.pow() will do it for you and it's what he is using in the end anyway :D

    I am aware that Math.pow() will do the work for you... but the OP didn't specify a language or if such handly libraries are available to him/her. My aim was to quickly throw together a method that would show the OP how a recursive function could be used to achieve their goal. They could then code something similar in their language of choice. At the risk of repeating myself (well in fact I am repeating myself...)
    stipey wrote:
    I just used Java because I had Eclipse open and I wanted to make sure the code actually ran before posting it.

    Finally, I am aware that it is possible to raise a number to a negative power. As I have already mentioned however, I was merely providing a simple example for the OP's benefit. I saw had neither the inclination nor the desire to complicate the code by having it handle all these scenarios solely to satisfy the pedants prowling the programming board. In actualy fact my reason for mentioning a check for a negative power argument was to prevent the method entering an infinite loop (i.e. it was more to the scaled down, incomplete nature of the method as opposed to any distorted mathematical reasoning).


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,079 Mod ✭✭✭✭AlmightyCushion


    stipey wrote:
    Man, you got it from all sides there - no offence meant, nothing personal and all that.

    Well not from me anyway - the other lads might have had ulterior motives :P

    Just admit it you're all out to get me.


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    professore wrote:
    C++ would be x ^ 3


    is the ^ operator not the XOR operator in C, and C++?? :o


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    is the ^ operator not the XOR operator in C, and C++?? :o
    yes, yes it is.


  • Advertisement
Advertisement