How to raise a double value by power of 12?

后端 未结 5 1073
谎友^
谎友^ 2020-12-11 06:49

I have a double which is:

double mydouble = 10;

and I want 10^12, so 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10. I tried

相关标签:
5条回答
  • 2020-12-11 06:57

    try pow(10.0, 12.0). Better yet, #include math.h.

    To clarify: If you don't include math.h, the compiler assumes that pow() returns an integer. Including math.h brings in a prototype like

    double pow(double, double);
    

    So the compiler can understand how to treat the arguments and the return value.

    0 讨论(0)
  • 2020-12-11 07:00

    did you try casting to a double:

    NSLog(@"(double)pow(10, 12)                    = %lf", (double)pow(10, 12));
    
    0 讨论(0)
  • 2020-12-11 07:02

    I couldn't even get the program to compile without:

    #include <math.h>
    

    When using math functions like this you should ALWAYS include math.h and make sure you are calling the right pow function. Who knows what the other pow function might be ... it could stand for "power wheels" haha

    0 讨论(0)
  • 2020-12-11 07:18

    Here's how to compute x^12 with the fewest number of multiplications.

    y = x*x*x; y *= y; y *= y;
    

    The method comes from Knuth's Seminumerical Algorithms, section 4.6.3.

    0 讨论(0)
  • 2020-12-11 07:18

    That's the right syntax for pow, what format string are you passing to NSLog(…)?

    0 讨论(0)
提交回复
热议问题