Results 1 to 4 of 4

Thread: Square Root

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Québec, Canada
    Posts
    131

    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));

    Khavoerm Irithyl

  2. #2
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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?

  3. #3
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width