0

In this program, I'm trying to calculate the square root with the newton equation 1/2(x+a/x) just using integers. So if I repeat this equation at least 10 times, it should divide the number by 1000 and give an approximate value of the square root of a/1000. This is the code:

int main (){
    int32_t a, x;  //integers a and x
    float root;

     do{
    scanf ("%d", &a);   // get value of a from the user
    
    
    if (a < 1000 ){   // if chosen number is less than 1000 the programm ends.
    break;
    }
    
    x = ((float) a / 1000);   //starting value of x is a / 1000;
    for (int i = 0; i < 50;i++)
    {

        root = ((float) x * (float) x + a/1000) / ((float)2*x);  // convert int to float //through casting
        x = (float)root;    // refresh the value of x to be the root of the last value.


    }
    printf ("%f\n", (float)root);
     }while (1);

    return 0;

}

so if I calculate the square root of 2000, it should give back the square root of 2(1.414..), but it just gives an approximate value: 1.50000 How can I correct this using integers and casting them with float? thanks

Adam
  • 1,254
  • 12
  • 25
Angie
  • 9
  • 4
  • `int32_t a, x;` can you instead define `a` and `x` as floats -> `float a, x;`, and `scanf ("%f", &a);`? should provide expected your expected value – chickity china chinese chicken Oct 23 '21 at 19:39
  • `a/1000` (integer divsion) is certinaly amiss. I'd expect `(float) a / 1000`. Angie, did you want integer division in `((float) x * (float) x + a/1000)`? – chux - Reinstate Monica Oct 24 '21 at 05:48
  • 1
    Could it be that you missed completely the intent of the task? As per the title, there should be no floating point numbers at all in the computation. As I see it, for the integer input `a` you have to do the Newton/Heron/Babylonian method in integer arithmetic for `1000*1000*a` to get an integer result close to `1000*sqrt(a)`, thus getting about 3 digits after the dot for `sqrt(a)`. – Lutz Lehmann Oct 24 '21 at 10:27
  • As said by @LutzLehmann, your approach is not "just using integers". –  Oct 24 '21 at 18:35

1 Answers1

-1
    #include <stdlib.h>
    #include <stdio.h>
    
    int main (int argc, char * *argv, char * *envp) {
        int32_t a, x;  //integers a and x
        float root;
    
        do {
            scanf ("%d", &a);   // get value of a from the user
            if (a < 1000) {   // if chosen number is less than 1000 the program ends.
                break;
            }
            x = (int32_t)((float) a / 1000.0f);   //starting value of x is a / 1000;
            for (int i = 0; i < 1000; i++) {
// Fixed formula based on (x+a/x)/2
                root = ((float)x + (((float)a) / (float)x)) / 2.0f;
//Below, wrong old formula
                //root = ((float) x * (float) x + a / 1000) / ((float) 2 * x);  // convert int to float //through casting
                x = (int32_t) root;    // refresh the value of x to be the root of the last value.
            }
            printf ("%f\n", root);
        } while (1);
    
        return (EXIT_SUCCESS);
    }
LORDTEK
  • 137
  • 11
  • 1
    This answer would be much more useful if you can explain what was wrong in the original code, and how the changes you made in your version fix the problem. – G. Sliepen Oct 23 '21 at 21:18
  • @G.Sliepen The formula says that: (x+a/x)/2 But he wrote it wrongly. I fixed the formula. – LORDTEK 32 secs ago – LORDTEK Oct 23 '21 at 21:22
  • 1
    Great! You should edit your answer and point that out. I would just show line with the error before and after the fix. – G. Sliepen Oct 23 '21 at 21:24
  • @G.Sliepen I put comments based on your comments. – LORDTEK Oct 24 '21 at 05:33
  • Thank you for your answer, but it still doesn't divide the number the user enters by 1000. So if I enter the number 2000 it should print out the value of the root square of 2 and not 2000. Anyways, thanks. – Angie Oct 24 '21 at 20:19