0

This is the code which I have written to find approx square root a non perfect number(in the order 0.0001 and also exact square root of a perfect square. It is working with non perfect square numbers but not perfect. When I put 25 it gives 5.000068

#include<stdio.h>
int main()
{
    float a,i,count;
    scanf("%f",&a);

    for(count=1;(1);count=count+0.0001)
    {
        i=count*count;
        if (i<=a)
        break;
    }
    printf("%f",count);
    return 0;
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Ishan Bansal
  • 99
  • 1
  • 8

3 Answers3

2

Your method is wrong for computing square roots.

You deserve a bad grade for not reading a bit about square roots. Understanding the problem is the first step for any software development.

First, you should read The Floating Point Guide and the classical What Every Programmer Should Know About Floating-Point Arithmetic. It explains why your program cannot work (i.e. gives inaccurate results).

Then, your program is very inefficient (for large input numbers like several billions, it takes an enormous amount of computing time; for very small numbers like 0.01 it probably never terminates). Learn about Newton-Raphson's method, perhaps by reading some basic math book.

Notice that many fixpoint computations translate to iterative algorithms.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

There are two things that are faulty with your code:

Floating point is not exact. If you want more accuracy, use double, not float.

Your increment value is too large. If you incremented by a smaller amount you would (or should) get the desired value: See here: http://ideone.com/7XM2IK

The next issue doesn't affect your code now, but be warned anyway:

Do not use floating point arithmetic as a loop counter. To fix this issue, normalize your loop to do integer counts, and do the floating point inside the loop:

int count;
float dCount = 1.0;
float i, a;
//...
for (count=0; count < 100000; ++count)
{
   //...
}

See this link: https://www.securecoding.cert.org/confluence/display/java/NUM09-J.+Do+not+use+floating-point+variables+as+loop+counters

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • 1
    In this case the problem is not the for loop, because the end-condition does not use the floating point variable at all. Your code would give the same result 5.000068 (where OP expects 5.0) – Martin R Sep 06 '14 at 05:15
  • Yep, the increment value is too large (0.0001). However this is a very naive implementation of finding a square root. – PaulMcKenzie Sep 06 '14 at 05:43
  • @BasileStarynkevitch I'm tired. It obviously cannot solve for input < 1.0, so I leave it to the OP to fix the issue (the solution is simple). – PaulMcKenzie Sep 06 '14 at 06:22
1

If you dont want to struggle with books and math:

#include <stdio.h>
#include <math.h>
int main()
{
  float n = 25;// ofc use scanf xD

  n=sqrt(n);

  printf("%.5f", n);


return 0;
}
user229044
  • 232,980
  • 40
  • 330
  • 338
Aleksa
  • 37
  • 1
  • 1
  • 7