Thread: Erroroneous output in calculating the square root of a number

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Erroroneous output in calculating the square root of a number

    Dear All,

    I have written a program to calculate the square root of an input number. It gives me an erroneous answer,for example, when I input the number 5, I get an answer 2.5 whereas I should get 2.24, same with other numbers. Below is the program, please point out bugs and fixes:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
        float error=.00001,g1=0.0,g2=0.0,m;
        printf("Enter number whose square root has to be calculated");
        scanf("%f",&m);
        g2=m/2;
        while (abs(g1-g2)<error)
        {
              g1=g2;
              g2=(g1+(m/g1))/2;
        }
        printf("The square root of entered number is %f",g2);
        system("pause");
        return 0;
    }

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You should use double instead of float for more precision. Also if I'm not mistaken, you should use fabs() instead of abs() on floating-point numbers.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you are not mistaken. abs will convert the return value to integer and should issue a compiler warning if warning levels are high enough which would catch this type of error.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Incorporated the changes that you mentioned....program still returning the wrong values...got 2.5 as the square root of 5...

    Quote Originally Posted by QuantumPete View Post
    You should use double instead of float for more precision. Also if I'm not mistaken, you should use fabs() instead of abs() on floating-point numbers.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You should learn to use youre debugger. Had you done that, you would have very easily noticed the problem by stepping through the code.

    Line 10 has the wrong comparison operator, so it is not entering the loop.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thanks...solved my problem.

    Quote Originally Posted by iMalc View Post
    You should learn to use youre debugger. Had you done that, you would have very easily noticed the problem by stepping through the code.

    Line 10 has the wrong comparison operator, so it is not entering the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code to find th square root of a number
    By CODY21 in forum C++ Programming
    Replies: 34
    Last Post: 10-29-2010, 09:27 AM
  2. Square Root of a number
    By freddyvorhees in forum C++ Programming
    Replies: 37
    Last Post: 08-01-2008, 01:08 PM
  3. Calculating Square Root?
    By Neo1 in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2007, 02:40 AM
  4. finding the square root of a number
    By Geo-Fry in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2003, 07:46 PM
  5. square root of a number in C
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-19-2001, 04:06 PM

Tags for this Thread