Does C++ have a squared operator?

Started by
20 comments, last by Bacterius 9 years, 10 months ago

I mean something like 2^2 = 4 or 10^2 = 100. I'm programming physics and I kinda need it. If you have code that replicate exponents in math I would like to ask if you could give it to me. Thanks all for any support.

Advertisement

No, there is no operator for squaring, why would there be when you can simply do it yourself with the multiplication operator. And std::pow for doing arbitrary powers (with some limitations)

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

I guess you're right. I thought of that right I made the thread. I'm still trying out physics and the math isn't coming together like I hoped. I'm thinking about how the compiler will process my code and it look pretty inaccurate to me.

There is not an operator per se, but there is the pow function , which raises a base to an exponent.

You know what I'm so sorry for making this thread. We have a math and physics sub forum and my thread will serve it's purpose better there. Thank for incoming me about POW. I will research it.

If you so want, you could always try creating a custom class that overloads the ^ operator, using pow internally happy.png

Don’t use pow() unless necessary; there is no guarantee the compiler will optimize it away into “X*X” and when it doesn’t you will have a major performance problem.
Just use X*X.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

I know most ppl here dont like macro, and i don't either most of the time, but i think that's a good case for one:

#define POW2(x) ((x)*(x))

or maybe an inline function if you dont like macros.

I know most ppl here dont like macro, and i don't either most of the time, but i think that's a good case for one:

#define POW2(x) ((x)*(x))

or maybe an inline function if you dont like macros.

This macro evaluates its operand twice though which is actually probably your enemy in situations where you would want to use this macro (compilers are not necessarily allowed to reorder operations or even do common subexpression elimination with floating-point math) so I would recommend against it.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

I know most ppl here dont like macro, and i don't either most of the time, but i think that's a good case for one:

#define POW2(x) ((x)*(x))

or maybe an inline function if you dont like macros.

My question is why you think this is good cause for a macro? What benefit does this provide over:

template <typename T>
T pow2(const T& x) {
  return x * x;
}

// Or, if using a more "modern" C++:
template <typename T>
constexpr T pow2(const T& x) {
  return x * x;
}
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement