Search code examples
c++visual-studio-2012rootcubic

Cubic root function cbrt() in Visual Studio 2012


I am writing a program in Visual Studio 2012 Professional (Windows) in C/C++ which consists of calculating many powers using pow(). I ran the profiler to find out why it takes such a long time to run and I found that pow() is the bottleneck.

I have rewritten the powers such as

pow(x,1.5) to x*sqrt(x)

and

pow(x,1.75) to sqrt(x*x*x*sqrt(x))

which significantly improved the speed of the program.

A few powers are of the kind pow(x,1.0/3.0) so I looked for the cubic root function cbrt() to speed up things but it seems not available in Visual Studio which I can hardly imagine so therefore my question:

Where can I find the cbrt() function in Visual Studio 2012 Professional and if not, what are the alternatives except for pow(x,1.0/3.0)?

Kind regards,

Ernst Jan


Solution

  • This site explores several computational methods to compute cube root efficiently in C, and has some source code you can download.

    (EDIT: A google search for "fast cube root" comes up with several more promising-looking hits.)

    Cube roots are a topic of interest, because they're used in many common formulae and a fast cube root function isn't included with Microsoft Visual Studio.

    In the absence of a special cube root function, a typical strategy is calculation via a power function (e.g., pow(x, 1.0/3.0)). This can be problematic in terms of speed and in terms of accuracy when negative numbers aren't handled properly.

    His site has some benchmarks on the methods used. All of them are much faster than pow().

    32-bit float tests
    ----------------------------------------
    cbrt_5f      8.8 ms    5 mbp   6.223 abp
    pow        144.5 ms   23 mbp  23.000 abp
    halley x 1  31.8 ms   15 mbp  18.961 abp
    halley x 2  59.0 ms   23 mbp  23.000 abp
    newton x 1  23.4 ms   10 mbp  12.525 abp
    newton x 2  48.9 ms   20 mbp  22.764 abp
    newton x 3  72.0 ms   23 mbp  23.000 abp
    newton x 4  89.6 ms   23 mbp  23.000 abp
    

    See the site for downloadable source.