Need some advices how to increse accuracy of the algorithm


 
Thread Tools Search this Thread
Top Forums Programming Need some advices how to increse accuracy of the algorithm
# 1  
Old 04-10-2011
Need some advices how to increse accuracy of the algorithm

Hi guys Smilie

Can you raccomand some what I can do to increse accuracy of the Newton-Raphons method. It's not very accurate.

Code:
// Implementation of Newton - Raphson method for
// determination square root od positive number 
// Date: 10. april 2011
// Author: Solaris_user 
// Solution for programming exercise 13

#include <stdio.h>

int main(void) {
    
    double tolerance, input, y, x = 1.0;
    
    // Ask user to enter some number to find square root 
    // if user enters negative number then re-ask for new input
    // input must be greater then numbers zero and one. 
    // also user must enter some tolerance which will controll loop 
    // test condition    

    printf("Newton - Raphson square root algorithm\n");
    
    do {
        printf("Enter some number to find squre root: ");
        scanf("%lf", &input);
        if (input <= 1) {
            printf("Can't find square root for input number\n");
            }
        } while (input < 1);
    
    do {
        printf("Enter desired tolerance ( < 0.01): ");
        scanf("%lf", &tolerance);
        if (tolerance >= 0.01) {
            printf("Too small tolerance\n");
            }
        } while (tolerance >= 0.01);
    
    // Newton - Raphson method: 
    // start with x = 1.0. Then set y = (x + input/2) / 2 . If x and y are not 
    // close enough then set x = y, stop when difference is small enough 
    // to be negligible. 
    
    do {
        y = (x + (input / 2) ) / 2;
        if (x > y ) {
            x = y;
            }
        } while ( (x - y) > tolerance);
    
    printf("Square root is %.7lf\n", y);    
    return 0;
}

This is my output

Code:
Solaris:~/Desktop> ./a.out
Newton - Raphson square root algorithm
Enter some number to find squre root: 9
Enter desired tolerance ( < 0.01): 0.00000001
Square root is 2.7500000

# 2  
Old 04-10-2011
Your code checks if x is greater than y, and if so sets x to y. Thus the quantity (x - y) must always be less than or equal to zero. I suspect you'll get better results if you change this:

Code:
    do {
        y = (x + (input / 2) ) / 2;
        if (x > y ) {
            x = y;
            }
        } while ( (x - y) > tolerance);

to this:
Code:
     do {
        y = (x + (input / ( double ) 2.0) ) / ( double ) 2.0;
        if (x > y ) {
            x = y;
            }
        } while ( (y - x) > tolerance);

Note that I also changed the integer values to floating point values and explicitly cast them to double. Using integers works because the integers are automatically promoted to floating point values, but offhand I don't know the rules for argument promotion, and I doubt that you do. And you NEVER want to write code that relies upon rules you don't understand. Especially number-crunching code like this.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Fedora

Accuracy of jobs scheduled in cron

Hello, I have several cron jobs scheduled but looking at the results of running I see in some cases it takes more than 2 seconds, is there any way to adjust the accuracy of execution of cron? Is there any other tool or way to fix the problem? (10 Replies)
Discussion started by: faka
10 Replies

2. Homework & Coursework Questions

Want to learn Shell well.. Advices

Hi everyone.. Thanks a lot for reading this post. I am trying to learn shell and Unix well. I am taking course at UNT school, unfortunately, the professor doesn't explain well. I am trying to take an advantage of this course and learn as much as I could. I learn by myself.. read the book... (1 Reply)
Discussion started by: smasm9
1 Replies

3. IP Networking

how to increse server lan speed in hp-ux

Hi, I've a problem in hp-ux server, i.e.how to increase Ethernet speed. i dont've time to trials on production server please help me (1 Reply)
Discussion started by: karlapudi.ramu
1 Replies

4. UNIX for Dummies Questions & Answers

accuracy of output - decimal points

Is there a way when using awk to specify the number of decimal points needed for the output? (2 Replies)
Discussion started by: cosmologist
2 Replies

5. Shell Programming and Scripting

Getting column with pinpoint accuracy

Hello scripters, what is the best way to parse 6.2mb in this line? i know how to get that column, but i wanna see if there is a better way to do it. Jun 22 2011 10:45:26 PROCESS NAME:httpd PID: 19910 CPU UTIL: 0.0% MEM UTIL: 6.2mb THREAD COUNT: 1 (2 Replies)
Discussion started by: ryandegreat25
2 Replies

6. Filesystems, Disks and Memory

Linux Storage system: looking for advices

Gidday! I'd like to setup a storage server for a friend of mine (he is a hobby photographer, and he produces about 100Gb pictures monthly). My friend has the following PC-Server-like system: AMD Athlon Dual Core Processor 4850e. ASUS M3N78-EMH HDMI motherboard with 6 SATA connectors. 3Gb... (7 Replies)
Discussion started by: Loic Domaigne
7 Replies

7. Shell Programming and Scripting

Need advices on scripting for remote servers

Hi guys, I need some advice and recommendations for a work project I am doing. Let me state that security is not a concern as this is a closed network and the data is not sensitive. Here's what I would like to do and how I was planning to accomplish it: I have an application on my remote... (1 Reply)
Discussion started by: blueicedrop2000
1 Replies

8. HP-UX

vmstat, comments and advices needed

Hi all.. I need some comments or advices about my HP-UX 11.11 performance. It runs Oracle, some apps and weblogic. For several times in a day, it's performance drops poorly (approx for minutes until an hour). I tried to find articles about unix, oracle and weblogic tuning, which leads me... (13 Replies)
Discussion started by: galapagos
13 Replies

9. UNIX for Dummies Questions & Answers

Error: when i try to Increse the size of LV

I have AIX 5.1, and one of my LV is 100% used... and i have PP's available to incresed the size... and When i try Increase the Size of a Logical Volume... the SMITTY CONSOLE display an error... ERROR MESSAGE Press Enter or Cancel to return to the application. 1820-037 An internal... (0 Replies)
Discussion started by: mgonzal
0 Replies
Login or Register to Ask a Question