problems with sqrt function

This is a discussion on problems with sqrt function within the C Programming forums, part of the General Programming Boards category; Dear All, I am terribly confused by something at the moment. If anyone could tell me what they think the ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    2

    problems with sqrt function

    Dear All,

    I am terribly confused by something at the moment. If anyone could tell me what they think the problem is that would be great.

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
        double a=(sqrt(4*M_PI*M_PI+1)/2),b=(sqrt(M_PI*M_PI+1/4));
        
        printf("a=%f\t\tb=%f",a,b);
        
        return(0);
    }
    As you can easily tell mathematically my definitions of a and b are identical. However when one runs the program a has the correct value stored, but b has the value M_PI.

    Thank you for all of your time.
    Edz

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This is because this is computers, not mathematics!

    C believes that 1/4 == 0, since it does integer division when dividing integers.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    1/4 = 0.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    try this way
    Code:
        double a=(sqrt(4.0*M_PI*M_PI+1.0)/2.0),b=(sqrt(M_PI*M_PI+1.0/4.0));
    Kurt

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    2
    Thank you all

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    you can also type cast by using the data type a constant is referring to..

    like:
    int(1.0)=1
    float(1)=1.0

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    M_PI is nonstandard, although GCC's <math.h> has it. For portability, use something like
    Code:
      const double Pi = 4.*atan(1.);
    instead.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Or
    Code:
    #ifndef M_PI
        #define M_PI 3.14159265358979324
    #endif
    (From memory, might want to check the value . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21