Why isn't `int pow(int base, int exponent)` in the standard C++ libraries?

why isn't my brain working supplements
why isn't my brain working pdf
why isn't my brain working audiobook
why isn't
why isn't my brain working review
why isn't my brain working reddit
why isn't my brain working pdf download
is netflix down or is it just me

I feel like I must just be unable to find it. Is there any reason that the C++ pow function does not implement the "power" function for anything except floats and doubles?

I know the implementation is trivial, I just feel like I'm doing work that should be in a standard library. A robust power function (i.e. handles overflow in some consistent, explicit way) is not fun to write.

Why isn't ventilation part of the conversation on reopening schools , Covid-19 is mainly transmitted through respiratory droplets in the air. Why isn't improving ventilation a key part of the effort to reopen schools? Black D.C. residents fought to change their city’s unequal status during the civil rights movement, and won some key victories.The first was the right to vote for the president and vice

Europe is going all in on hydrogen power. Why isn't the US?, This post has been updated with a correction. Once upon a time, hydrogen was supposed to be the future of energy. In 1845, English scientist� On the ultraprocessed diet, the subjects on average consumed 500 more calories a day and gained two pounds. A possible reason: The participants’ levels of PYY, a hormone that suppresses appetite

Because there's no way to represent all integer powers in an int anyways:

>>> print 2**-4
0.0625

Why Isn't Netflix Working, Why isn't Netflix working? If Netflix isn't working, you may be experiencing a network connectivity issue, an issue with your device, or an issue� Why Isn’t The Media Interviewing Portland’s Rioters? July 23, 2020 By David Marcus. Among the many confusions beset upon our boondoggled society at the moment is the question of just who the

That's actually an interesting question. One argument I haven't found in the discussion is the simple lack of obvious return values for the arguments. Let's count the ways the hypthetical int pow_int(int, int) function could fail.

  1. Overflow
  2. Result undefined pow_int(0,0)
  3. Result can't be represented pow_int(2,-1)

The function has at least 2 failure modes. Integers can't represent these values, the behaviour of the function in these cases would need to be defined by the standard - and programmers would need to be aware of how exactly the function handles these cases.

Overall leaving the function out seems like the only sensible option. The programmer can use the floating point version with all the error reporting available instead.

Why Isn't My Brain Working?: A revolutionary , Amazon.com: Why Isn't My Brain Working?: A revolutionary understanding of brain decline and effective strategies to recover your brain's health eBook:� But that isn’t true for every interesting set of numbers. As an example, let’s look at the set of numbers of the form a + b √-5, or a +i b √5, where a and b are both integers and i is the

Short answer:

A specialisation of pow(x, n) to where n is a natural number is often useful for time performance. But the standard library's generic pow() still works pretty (surprisingly!) well for this purpose and it is absolutely critical to include as little as possible in the standard C library so it can be made as portable and as easy to implement as possible. On the other hand, that doesn't stop it at all from being in the C++ standard library or the STL, which I'm pretty sure nobody is planning on using in some kind of embedded platform.

Now, for the long answer.

pow(x, n) can be made much faster in many cases by specialising n to a natural number. I have had to use my own implementation of this function for almost every program I write (but I write a lot of mathematical programs in C). The specialised operation can be done in O(log(n)) time, but when n is small, a simpler linear version can be faster. Here are implementations of both:

    // Computes x^n, where n is a natural number.
    double pown(double x, unsigned n)
    {
        double y = 1;
        // n = 2*d + r. x^n = (x^2)^d * x^r.
        unsigned d = n >> 1;
        unsigned r = n & 1;
        double x_2_d = d == 0? 1 : pown(x*x, d);
        double x_r = r == 0? 1 : x;
        return x_2_d*x_r;
    }
    // The linear implementation.
    double pown_l(double x, unsigned n)
    {
        double y = 1;
        for (unsigned i = 0; i < n; i++)
            y *= x;
        return y;
    }

(I left x and the return value as doubles because the result of pow(double x, unsigned n) will fit in a double about as often as pow(double, double) will.)

(Yes, pown is recursive, but breaking the stack is absolutely impossible since the maximum stack size will roughly equal log_2(n) and n is an integer. If n is a 64-bit integer, that gives you a maximum stack size of about 64. No hardware has such extreme memory limitations, except for some dodgy PICs with hardware stacks that only go 3 to 8 function calls deep.)

As for performance, you'll be surprised by what a garden variety pow(double, double) is capable of. I tested a hundred million iterations on my 5-year-old IBM Thinkpad with x equal to the iteration number and n equal to 10. In this scenario, pown_l won. glibc pow() took 12.0 user seconds, pown took 7.4 user seconds, and pown_l took only 6.5 user seconds. So that's not too surprising. We were more or less expecting this.

Then, I let x be constant (I set it to 2.5), and I looped n from 0 to 19 a hundred million times. This time, quite unexpectedly, glibc pow won, and by a landslide! It took only 2.0 user seconds. My pown took 9.6 seconds, and pown_l took 12.2 seconds. What happened here? I did another test to find out.

I did the same thing as above only with x equal to a million. This time, pown won at 9.6s. pown_l took 12.2s and glibc pow took 16.3s. Now, it's clear! glibc pow performs better than the three when x is low, but worst when x is high. When x is high, pown_l performs best when n is low, and pown performs best when x is high.

So here are three different algorithms, each capable of performing better than the others under the right circumstances. So, ultimately, which to use most likely depends on how you're planning on using pow, but using the right version is worth it, and having all of the versions is nice. In fact, you could even automate the choice of algorithm with a function like this:

double pown_auto(double x, unsigned n, double x_expected, unsigned n_expected) {
    if (x_expected < x_threshold)
        return pow(x, n);
    if (n_expected < n_threshold)
        return pown_l(x, n);
    return pown(x, n);
}

As long as x_expected and n_expected are constants decided at compile time, along with possibly some other caveats, an optimising compiler worth its salt will automatically remove the entire pown_auto function call and replace it with the appropriate choice of the three algorithms. (Now, if you are actually going to attempt to use this, you'll probably have to toy with it a little, because I didn't exactly try compiling what I'd written above. ;))

On the other hand, glibc pow does work and glibc is big enough already. The C standard is supposed to be portable, including to various embedded devices (in fact embedded developers everywhere generally agree that glibc is already too big for them), and it can't be portable if for every simple math function it needs to include every alternative algorithm that might be of use. So, that's why it isn't in the C standard.

footnote: In the time performance testing, I gave my functions relatively generous optimisation flags (-s -O2) that are likely to be comparable to, if not worse than, what was likely used to compile glibc on my system (archlinux), so the results are probably fair. For a more rigorous test, I'd have to compile glibc myself and I reeeally don't feel like doing that. I used to use Gentoo, so I remember how long it takes, even when the task is automated. The results are conclusive (or rather inconclusive) enough for me. You're of course welcome to do this yourself.

Bonus round: A specialisation of pow(x, n) to all integers is instrumental if an exact integer output is required, which does happen. Consider allocating memory for an N-dimensional array with p^N elements. Getting p^N off even by one will result in a possibly randomly occurring segfault.

Why Isn't My Brain Working?: A Revolutionary Understanding of , Why Isn't My Brain Working?: A Revolutionary Understanding of Brain Decline and Effective Strategies to Recover Your Brain's Health: 8601420672192:� Why isn't Netflix working? If Netflix isn't working, you may be experiencing a network connectivity issue, an issue with your device, or an issue with your Netflix

Why isn't the Netherlands underwater? - Stefan Al, Dig into the incredible engineering of the Netherlands' Delta Works— the most sophisticated Duration: 5:24 Posted: Mar 24, 2020 Why Isn't My Printer Printing? 6 printing issues you can fix. by. Tom Nelson. Writer. Tom Nelson is an engineer, programmer, network manager, and computer network and

Why isn't my app working?, Why isn't my app working? If you're having issues with your device or app, try these troubleshooting tricks: Try force-closing the app and reopening. Make sure � The Most Common Reason Your Disk Is Missing. You grabbed a nice big hard disk on sale, you cracked open your computer case, plugged the drive into the motherboard and power supply with the appropriate cables (no? better double check that before you keep reading), and when you booted your computer back up the new hard drive was nowhere to be found.

Why Isn't California Criticized Like Florida on Covid-19?, It's a blue state, of course. But the virus doesn't discriminate based on party affiliation. For owners, a dog that isn't eating is a common concern. There are a variety of reasons why dogs won't eat, but some reasons are more serious than others.

Comments
  • This is a good question, and I don't think the answers make a lot of sense. Negative exponents don't work? Take unsigned ints as exponents. Most inputs cause it to overflow? The same is true for exp and double pow, I don't see anyone complaining. So why isn't this function standard?
  • @static_rtti: "The same is true for exp and double pow" is totally false. I will elaborate in my answer.
  • The standard C++ library has double pow(int base, int exponent) since C++11 (§26.8[c.math]/11 bullet point 2)
  • You need to make up your mind between 'the implementation is trivial' and 'not fun to write'.
  • FWIW, I don't think C++ follows "Provide only one way to do an operation" as a constraint. Rightly so, because for example to_string and lambdas are both conveniences for things you could do already. I suppose one could interpret "only one way to do an operation" very loosely to allow both of those, and at the same time to allow almost any duplication of functionality that one can imagine, by saying "aha! no! because the convenience makes it a subtly different operation from the precisely-equivalent but more long-winded alternative!". Which is certainly true of lambdas.
  • @Steve, yes, that was badly worded on my part. It's more accurate to say that there are guidelines for each committee rather than all committees follow the same guidelines. Adjusted answer to clarifyl
  • Just one point (out of a few): "any code monkey could whip up in ten minutes". Sure, and if 100 code monkeys (nice insulting term, BTW) do that each year (probably a low estimate), we have 1000 minutes wasted. Very efficient, don't you think?
  • @Jürgen , it wasn't meant to be insulting (since I didn't actually ascribe the label to anyone specific), it was just an indication that pow doesn't really require much skill. Certainly I'd rather have the standard provide something which would require a lot of skill, and result in far more wasted minutes if the effort had to be duplicated.
  • @eharo2, just replace the "half decent coder" in the current text with "code monkey". I didn't think it was insulting either but I thought it best to be cautious and, to be honest, the current wording gets across the same idea.
  • Anyway all of this is moot. Just because a function isn't valid for some or a lot of inputs doesn't make it less useful.