Truncation problem?

My friend challenged me to make a calculator. I decided to make an exponent function, and it works fine except for when I enter a fraction. I'm using the pow() function which is supposed to be able to take long doubles. I even tested it with constants on line 30 and it outputs the square root of 2, exactly as it should. But If I enter:
2^.5=

It gives me 1 which is what I would get as though 0.5 was truncated. What's going on here?

It's too long to be put on the forum, so here's a pastebin of it.
http://pastebin.com/LTTvTvLB


Nevermind. My bad. I figured it out.
Last edited on
The arguments to the pow function should be doubles, however I would have expected the compiler to implicitly cast the int to a double.

IMO it is better to explicitly specify the arguments as double:

test = pow(2.0, 0.5);

There is also std::pow which does the right thing regardless of the type of the arguments.

HTH :)
Topic archived. No new replies allowed.