views:

285

answers:

4

I am taking a class in C++ and I noticed there are only a few math operators to use. I also noticed that C++ does not come with an exponential operator within its math library.

Why must one always write a function for this? Is there a reason for the makers o C++ to omit this operand?

Thanks

+6  A: 

There are pow and exp functions in math library.

ArunSaha
I just learned this after much "Google-ing". :)
James Hayek
+20  A: 

You don't write a function for this (unless you're mad, of course). There's a perfectly good pow function defined in cmath.

Aside: if you try to use ^ as a power operator, you'll be in for a nasty surprise :-)

paxdiablo
+1: Both of us pointed to the same link :)
ArunSaha
the lovely ^ trap ;)
Marlon
It only works for `1^0`.
dan04
Ha! I just found out that ^ is an OR operator ;)
James Hayek
Its *NOT* OR, its XOR :) See: http://en.wikipedia.org/wiki/Bitwise_operation#XOR
ArunSaha
+7  A: 

Most C operations readily intended to mapped to a single processor instruction when C was invented. At the time, exponentiation was not a machine instruction, thus the library routine.

msw
"Intended" may be too strong. C *did* map fairly directly onto the instruction sets of common architectures of the era, but I've never read of K *or* R saying "we can't have that feature, it's not something the machine does...".
dmckee
In fact, float-to-int casts and back are implicit, i.e. don't require an operation, but they were far more expensive than a single processor instruction.
MSalters
A: 

What platform and which compiler are you using? My guess is TurboC. Mostly cmath file has most of the mathematical functions covered in other compilers.

Manoj R
XCode at home, Visual Studio at school.
James Hayek