Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
1 2 3 4 5 6 7 8 9 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } #include<stdio.h> #include<stdlib.h> int main() { int answer = (int)(my_sqrt(12345, .001)*10); int expected = 1111; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
All tests passed
main.c: In function 'my_sqrt':
main.c:5:12: warning: implicit declaration of function 'fabs' [-Wimplicit-function-declaration]
5 | while (fabs(guess * guess - number) >= precision) {
| ^~~~
main.c:1:1: note: include '<math.h>' or provide a declaration of 'fabs'
+++ |+#include <math.h>
1 |
main.c:5:12: warning: incompatible implicit declaration of built-in function 'fabs' [-Wbuiltin-declaration-mismatch]
5 | while (fabs(guess * guess - number) >= precision) {
| ^~~~
main.c:5:12: note: include '<math.h>' or provide a declaration of 'fabs'
Testing if the previous output contains the string All tests passed
: True
Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
1 2 3 4 5 6 7 8 9 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } #include<stdio.h> #include<stdlib.h> int main() { int answer = (int)(my_sqrt(12345, .001)*10); int expected = 1111; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
All tests passed
main.c: In function 'my_sqrt':
main.c:5:12: warning: implicit declaration of function 'fabs' [-Wimplicit-function-declaration]
5 | while (fabs(guess * guess - number) >= precision) {
| ^~~~
main.c:1:1: note: include '<math.h>' or provide a declaration of 'fabs'
+++ |+#include <math.h>
1 |
main.c:5:12: warning: incompatible implicit declaration of built-in function 'fabs' [-Wbuiltin-declaration-mismatch]
5 | while (fabs(guess * guess - number) >= precision) {
| ^~~~
main.c:5:12: note: include '<math.h>' or provide a declaration of 'fabs'
Testing if the previous output contains the string All tests passed
: True
Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
1 2 3 4 5 6 7 8 9 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } #include<stdio.h> #include<stdlib.h> int main() { int answer = (int)(my_sqrt(12345, .001)*10); int expected = 1111; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
All tests passed
main.c: In function 'my_sqrt':
main.c:5:12: warning: implicit declaration of function 'fabs' [-Wimplicit-function-declaration]
5 | while (fabs(guess * guess - number) >= precision) {
| ^~~~
main.c:1:1: note: include '<math.h>' or provide a declaration of 'fabs'
+++ |+#include <math.h>
1 |
main.c:5:12: warning: incompatible implicit declaration of built-in function 'fabs' [-Wbuiltin-declaration-mismatch]
5 | while (fabs(guess * guess - number) >= precision) {
| ^~~~
main.c:5:12: note: include '<math.h>' or provide a declaration of 'fabs'
Testing if the previous output contains the string All tests passed
: True
Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
1 2 3 4 5 6 7 8 9 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | float my_sqrt(float number, float precision) { float guess = number / 2; // Start with an initial guess while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; // Improve the guess using the Babylonian method } return guess; } #include<stdio.h> #include<stdlib.h> int main() { int answer = (int)(my_sqrt(12345, .001)*10); int expected = 1111; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
All tests passed
main.c: In function 'my_sqrt':
main.c:5:12: warning: implicit declaration of function 'fabs' [-Wimplicit-function-declaration]
5 | while (fabs(guess * guess - number) >= precision) {
| ^~~~
main.c:1:1: note: include '<math.h>' or provide a declaration of 'fabs'
+++ |+#include <math.h>
1 |
main.c:5:12: warning: incompatible implicit declaration of built-in function 'fabs' [-Wbuiltin-declaration-mismatch]
5 | while (fabs(guess * guess - number) >= precision) {
| ^~~~
main.c:5:12: note: include '<math.h>' or provide a declaration of 'fabs'
Testing if the previous output contains the string All tests passed
: True
Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
Here's a python program. Please rewrite it in c with the type signature float my_sqrt(float, float). def my_sqrt(number, precision=0.0001): guess = number / 2 # Start with an initial guess while abs(guess * guess - number) >= precision: guess = (guess + number / guess) / 2 # Improve the guess using the Babylonian method return guess
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #include <math.h> float my_sqrt(float number, float precision) { float guess = number / 2; while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; } return guess; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> #include <math.h> float my_sqrt(float number, float precision) { float guess = number / 2; while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; } return guess; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <stdio.h> #include <math.h> float my_sqrt(float number, float precision) { float guess = number / 2; while (fabs(guess * guess - number) >= precision) { guess = (guess + number / guess) / 2; } return guess; } #include<stdio.h> #include<stdlib.h> int main() { int answer = (int)(my_sqrt(12345, .001)*10); int expected = 1111; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
All tests passed
Testing if the previous output contains the string All tests passed
: True