Small error getting to me sqrt function - c

I am writing this program to find the square root of a number. While defining the function mysqrt(). I am using the Newton-Raphson method. in the if() I use fabs(blah data blah) < 0.001 for relative error EX. if error is less than .1% the answer is correct. When I compile the code below
I get error line 27: called object '5.0e-1' is not a function.
I am absolutely lost as to what it is referring to. Please help Thank you.
I know there is a sqrt() function but this is for an extra credit assignment I have the majority of it written but cannot correct this error.
#include <stdio.h>
#include <math.h>
double mysqrt(double a);
int main()
{
double a, result;
printf("Enter a number to find the square root of: ");
scanf("%d", &a);
result = mysqrt(a);
printf("The square root of %d is %d \n", a, result);
}
double mysqrt(double num)
{
double x, new_x;
if((fabs(((x+1) - x)/(x+1)))<0.001)
{
for(x=2; x != num;)
{
new_x=(x+1);
new_x=(0.5(x+(num/x)));
x=new_x;
}
}
else
{
return new_x;
}
}

You forgot multiplication sign here:
new_x=(0.5(x+(num/x)));
This is a quite common error and when you see "... is not a function.", it means somewhere you have ...() which is most often a forgotten multiplication.
Also your program hard to read because of lots of parentheses, why not to write:
new_x = 0.5 * (x + num/x);
In addition, this:
(fabs(((x+1) - x)/(x+1))) < 0.001
is equivalent to:
fabs(1/(x+1)) < 0.001
Do your simplifications on paper, it will make your program more readable and errors easier to spot.

Related

why does it show segmentation fault when I tried to use recursion here?

I tried to write a code to calculate how many 1 are there in a number's binary form. This is my code:
#include <stdio.h>
#include <math.h>
static int num = 0;
void binary(int target){
int n = 0;
int a = 1;
if(target != 0){
while(target >= a ){
n++;
a = pow(2, n);
}
a = pow(2, n-1);
num++;
binary(target - a);
}
}
int main() {
int target = 0;
scanf("%d", &target);
binary(target);
printf("%d",num);
return 0;
}
However, this shows segmentation fault when I run it. I don't know where has the code tried to access memories that are not allowed. I figured it might have something to do with the recursion in the binary function. Can anyone tell me what have caused the segmentation fault here? Thank you so much. I really can't understand segfaults :(
As mentioned in the comments, pow is not a good candiate here due to its signature:
double pow(double x, double y);
so any place that you are using pow, you are implicitly using floating point numbers. I was able to cause a segfault with the input 1<<31 which is the value -2147483648. This will cause your loop to terminate with n=0, a=1. You then set a = pow(2, -1), but since a is an integer, this gets floored down to just 0. You then recurse with binary(target - 0) which might as well just be binary(target) again, hence you have an infinite call with no termination.
I'll also leave as a note that recursion for this type of problem is probably not the right tool, unless your goal is to learn about recursion. There is a much more concise and reliable method via a loop and the & operator. I would also suggest using unsigned values to avoid issues like this with negative terms.

How to get the exact output "math error"?

Actually i am trying to get exact output "math error"when i run the program i can't exactly finf what i am actually looking for.
#include <stdio.h>
#include<math.h>
int
main ()
{
int a,b,c,d,m,n;
float x1,x2;
printf("enter value of integer :");
scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&m,&n);
x1=(m*d-b*n)/(a*d-c-d);
x2=(n*a-m*c)/(a*d-c*d);
if(a*d-c*d==0)
{
printf("Math error");
}
else
{
printf("value of x1=%f\n",x1);
printf("value of x2=%f\n",x2);
}
return 0;
}
It looks like you're checking whether a*d-c*d==0 after you've used it as the denominator in the expressions to calculate x1 and x2. If that division causes a runtime divide-by-zero error the program might be crashing with other error output before it ever gets to your check.
To avoid that, you can put your if block above the lines where x1 and x2 are calculated. You should also end the program in the event of the error since further computation is invalid in that case. Of course, you need to keep the code in the else block below the lines that compute x1 and x2; you can just get rid of the else since at this point you know for sure the computation is valid.
Note: choosing d and c = 0 guarantees that your denominators will be zero (the denominators are actually different, I assume this is a typo, but c=0 and d=0 makes them both zero anyway).
You are doing integer division (n*a-m*c)/(a*d-c*d) before the check a*d-c*d==0, so you cannot avoid division by zero to get "Math error" printed.
Also what is in the printf is "Math error" with large M, not expected "math error" with small m.
Try this:
#include <stdio.h>
#include<math.h>
int
main ()
{
int a,b,c,d,m,n;
float x1,x2;
printf("enter value of integer :");
if(scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&m,&n)!=6)
{
printf("input error");
}
else
if(a*d-c*d==0)
{
printf("math error");
}
else
{
x1=(m*d-b*n)/(float)(a*d-c*d);
x2=(n*a-m*c)/(float)(a*d-c*d);
printf("value of x1=%f\n",x1);
printf("value of x2=%f\n",x2);
}
return 0;
}
What I fixed:
Added checking if the input is successful.
Moved the divisions after the check a*d-c*d==0.
Fixed the message "math error".
Changed a*d-c-d to a*d-c*d because I think it is what you want.
Added casting to float to avoid truncations which you may not like.

Square root values not being retrieved

I'm having a problem with getting my code to pick up the user input of the variables number and result. The correct way the code should act is: When user puts in a positive value, code should square root this value and display the new value. As of now, there are no error messages, the code runs fine, just not the way i want it to.
It does NOT pick up any value and does not square root math either, when looking at the output.
I also need to use pointers when doing this code (weird imo). Pointers are something im very new to so i expect to get quite the critique on that department.
I have tried reading several tutorials on pointers and the understanding of sqrt but it feels like key parts of those things still confuse me.
Code: (Consists of a function squareRoot that does the math and main that handles input/output)
#include <stdio.h>
#include <math.h> //needed for sqrt()
#define POSITIVE 1 //not used atm
#define NEGATIVE 0 //not used atm
float squareRoot(float number, float * result) {
return * result = sqrt(number); //calculate the square root of a number
}
int main() {
float number = 0;
float * result = malloc(sizeof(float));
printf("Enter a float value: ");
scanf("%f", &number);
squareRoot(number, result);
if (number < 0) {
printf("Square root of a negative value is not possible.");
}
if (number > 0) {
printf("Square root if %.2f is: %.2f "), number, result;
}
return 0;
}
Any help or constructive criticism is greatly appreciated!
result is a pointer, you need to dereference it.
printf("Square root if %.2f is: %.2f ", number, *result);
If you didn't get a compiler warning for the type mismatch, increase your warning level.

Why does my C program keep giving me 1 as the error?

I ran it, and everything seems to be fine--except that it keeps giving me a margin of error of 1. Why is it doing this?
The program is supposed to prompt the user to input an estimation for the cube root of 3, and it uses Newton's method of approximation to show how many attempts it took to get to the approximation. After 500 attempts or a margin of error less than 0.000001, it's supposed to exit the loop. Why, though, doesn't the margin of error change?
Here's my code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
float a, i, e; //declare float variables
printf("Consider the function f(x) = x^3 - 3 = 0.\n");
printf("Simplifying, we get x^3 = 3.\n");
printf("Simplifying it further, we get x = 3^(1/3).\n");
printf("Enter your estimate of the root: ");
scanf("%f", &a); //prompt user to guestimate
printf("So you're saying that x = %f.\n", a);
i=0; //initiate attempt counter
e=abs((a-pow(3, (1/3)))/pow(3, (1/3))); //margin of error formula
while (e>=0.000001 && i<=500) //initiate while loop with above expressions
{
if (a!=pow(3, (1/3)))
{
printf("Attempt %f: ", i);
a = a - (pow(a, 3) - 3)/(3*pow(a, 2));
printf("%f, ", a);
printf("%f margin of error\n", e);
i=i+1;
}
else
break;
}
}
abs() deals with ints and will return an int, you need fabsf().
In the same way, pow() is for doubles, you should use powf().
Another mistake is writing 1/3 and expecting 0.333... as a result. 1 and 3 are int literals, so the operation performed is integer division. You need to use float literals, such as 1.0f/3.0f.
That's it for type compatibility. I can see another error however : you expect e to somehow remember its formula and reapply it automagically. That's not how imperative languages work : when you write e = something, "something" is calculated and stored in eĀ once and for all. You're doing it correctly for a, now just bring e=abs(...); inside the while loop to update it each time.

simplify complex roots

i have made a program to compute roots of quauation but it does not simplify the roots.can anyone help me to simplify them
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
int a,b,c;
float d,d2;
printf(" Enter a,b and c:");
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("(%d+i%d)/%d\n",-b,sqrt(-d),2*a) ;
printf("(%d-i%d)/%d\n",-b,sqrt(-d),2*a);
}
else
{
printf("(%d+%d)/%d\n",-b,sqrt(d),2*a);
printf("(%d-%d)/%d\n",-b,sqrt(d),2*a);
}
getch();
}
You can't compute the square root of a negative number. d is negative and you're trying to find its square root. The whole point of complex solutions and the imaginary unit i is to write -1 as i^2, and then when d < 0 you have:
sqrt(d) = sqrt(i^2 * (-d)) = i*sqrt(-d)
So change to this:
if(d<0)
{
printf("(%d+i%lf)/%d",-b,sqrt(-d),2*a);
printf("(%d-i%lf)/%d",-b,sqrt(-d),2*a);
}
I don't know why you had parantheses around your printf arguments, I removed those.
The second %d should also be changed to %lf since sqrt returns a double.
If you want to compute square roots tof negative numbers, find a C99 compiler (basically, anything besides MSVC will do), include <complex.h> header, use complex data type and csqrt function.
http://en.wikipedia.org/wiki/Complex.h

Resources