Why is my power operator (^) not working?
Well, first off, the ^
operator in C/C++ is the bit-wise XOR. It has nothing to do with powers.
Now, regarding your problem with using the pow()
function, some googling shows that casting one of the arguments to double helps:
result = (int) pow((double) a,i);
Note that I also cast the result to int
as all pow()
overloads return double, not int
. I don't have a MS compiler available so I couldn't check the code above, though.
Since C99, there are also float
and long double
functions called powf
and powl
respectively, if that is of any help.
VBA power operator (^) not working as expected in 64-bit VBA [duplicate]
Confirmed: Also happens in MS Word 2010, 2013, 2016 all 64 bit Office*1
*1 I suspect this applies to most of, if not all, the 64 bit office applications
This seems to be an issue with the 64 bit version of MS Office. In the 32 bit version the caret ^
is used for the power operator, however in the 64 bit operator it is also used for variables of type LongLong (docs.microsoft.com/.../vba/.../longlong-data-type).
In 64 bit VBA, when using Debug.Print
*2 the IDE auto corrects 2^2
to 2^; 2
but 2 ^2
is auto-corrected to 2 ^ 2
; making the code compile-able.
By way of a solution, this is just one of those annoying things that users of 64bit VBA 'just need to know'.
Edit: Microsoft confirmed issue https://support.microsoft.com/en-gb/help/2454019/the-64-bit-office-2010-vba-compiler-shows-an-error-when-it-encounters
*2 I've also come to learn that the VBA behavior is different when using debug.Print
than assigning a variable (thanks to @ErikvonAsmuth)
Is there a exponent operator in C? [duplicate]
No, there isn't such operator in C. There are functions for this: pow()
, powf()
, powl()
(respectively for double
, float
and long double
) defined in header math.h
First parameter is base, second exponent.
Of course, them being defined for floating point types, doesn't mean you cannot use them for integers.
int x = pow(10, 2);
The result of pow()
will be properly cast from 100.0
to 100
and assigned to integer variable x
Why can't I use the ^ operator in C?
It works just fine and means bitwise XOR. That is, 1^2
gives 3.
Unfortunately C doesn't provide a function to take power of integers. This is a known flaw of the language. You have to roll out such a function yourself either by using multiplication in a loop, or use the slow floating point function pow
.
To the power of in C? [duplicate]
You need pow();
function from math.h
header.
syntax
#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
Here x is base and y is exponent. result is x^y
.
usage
pow(2,4);
result is 2^4 = 16. //this is math notation only
// In c ^ is a bitwise operator
And make sure you include math.h
to avoid warning ("incompatible implicit declaration of built in function 'pow'
").
Link math library by using -lm
while compiling. This is dependent on Your environment.
For example if you use Windows it's not required to do so, but it is in UNIX based systems.
Why doesn't C++ have a power operator?
C++ does have a power operator—it's written pow(x, y)
.
Originally, C was designed with system software in mind, and
there wasn't much need for a power operator. (But it has
bitwise operators, like &
and |
, which are absent in a lot
of other languages.) There was some discussion of adding one
during standardization of C++, but the final consensus was more
or less:
It couldn't be
^
, because the priority was wrong (and of
course, having2. ^ 8 == 256.
, but2 ^ 8 == 10
isn't very
pleasant either).It couldn't be
**
, because that would break existing
programs (which might have something likex**p
, withx
anint
, andp
anint*
).It could be
*^
, because this sequence isn't currently legal
in C or C++. But this would still require introducing an
additional level of precedence.C and C++ already had enough special tokens and levels of
precedence, and after discussions with the numerics community,
it was concluded that there really wasn't anything wrong withpow(x, y)
.
So C++ left things as they were, and this doesn't seem to have
caused any problems.
Calculation error with pow operator
According to docs, **
has higher precedence than -
, thus your code is equivalent to -(2 ** 2)
. To get the desired result you could put -2
into parentheses
>>> (-2) ** 2
4
or use built-in pow
function
>>> pow(-2, 2)
4
or math.pow
function (returning float
value)
>>> import math
>>> math.pow(-2, 2)
4.0
Related Topics
What Is the Type of String Literals in C and C++
C++ Unordered_Map Using a Custom Class Type as the Key
How to Iterate Over the Elements of an Std::Tuple
Returning Multiple Values from a C++ Function
What Do the Following Phrases Mean in C++: Zero-, Default- and Value-Initialization
Difference Between New/Delete and Malloc/Free
In C++, What Is a Virtual Base Class
Order of Evaluation in C++ Function Parameters
How to Remove Code Duplication Between Similar Const and Non-Const Member Functions
Derived Template-Class Access to Base-Class Member-Data
Thou Shalt Not Inherit from Std::Vector
How to Enable C++11/C++0X Support in Eclipse Cdt
What Are the Rules For Calling the Base Class Constructor
Array Index Out of Bound Behavior
Pass Parameter to Base Class Constructor While Creating Derived Class Object