Download code

Jump to: navigation, search

Back to Newton-Raphson's_method_for_root_finding_(C)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

newton_sqrt.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Newton-Raphson's_method_for_root_finding_(C)?oldid=5963
14 */
15 
16 #include <stdio.h>
17 
18 double newton_sqrt(double n)
19 {
20   double x = 0.0;
21   double xn = 0.0;
22   int iters = 0;
23   int i;
24   for (i = 0; i <= (int)n; ++i)
25   {
26     double val = i*i-n;
27     if (val == 0.0)
28       return i;
29     if (val > 0.0)
30     {
31       xn = (i+(i-1))/2.0;
32       break;
33     }
34   }
35   while (!(iters++ >= 100@ text
36 
37 	    || x == xn@ text
38 ))
39   {
40     x = xn;
41     xn = x - (x * x - n) / (2 * x);
42   }
43 
44   return xn;
45 }
46 
47 int main(void)
48 {
49   printf("Square root of 5: %f\n", newton_sqrt(5));
50   printf("Square root of 25: %f\n", newton_sqrt(25));
51   printf("Square root of 20: %f\n", newton_sqrt(20));
52   return 0;
53 }


hijacker
hijacker
hijacker
hijacker

build.log

1 /tmp/litprog754415/newton_sqrt.c: In function 'newton_sqrt':
2 /tmp/litprog754415/newton_sqrt.c:35:3: error: stray '@' in program
3 /tmp/litprog754415/newton_sqrt.c:35:28: error: expected ')' before 'text'
4 /tmp/litprog754415/newton_sqrt.c:37:6: error: stray '@' in program