|
-
Dec 11th, 2001, 06:02 PM
#1
Thread Starter
Addicted Member
Square Root
Hello,
For my project I created a COperation class. One of the function of COperation is SQR which should return the square root. Whatever the number I try, it gave me 1. What's wrong with my code?
PHP Code:
double COperation::SQR(double Number)
{
return pow(Number,(1/2));
}
-
Dec 11th, 2001, 06:13 PM
#2
Fanatic Member
ur multiplying it by a half
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Dec 11th, 2001, 06:33 PM
#3
Fanatic Member
If you are gonna use cmath, just use the sqrt function.
PHP Code:
double COperation::SQR(double Number)
{
return sqrt(Number);
}
Alcohol & calculus don't mix.
Never drink & derive.
-
Dec 12th, 2001, 12:28 PM
#4
1/2 is an integer division, you get an integer as result. Just like you do in elementary school:
1 divided by 2 is 0, 1 remains.
So you actually do
pow(Number, 0);
which is, of course, 1.
You COULD do
pow(Number, (1.0/2.0);
which does a floating point division, but this is BAD, because you force the cpu to do an operation you easily could do in your head (unless the compiler optimizes it away), so do either
pow(Number, 0.5);
or
sqrt(Number);
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|