geometric series in c, wrong solution - c

I'm getting a wrong solution for this series: (-1/4)^(n+1)*(z-1)^n
For |z-1|<4 should the series tend to converge to -1/3+z
For z=0.5 should be the solution -2/7, but if i try to plot with c, the result is 0...
Here is my code:
#include <stdio.h>
#include <math.h>
int main(){
double sum=0;
int n;
for(n=0;n<=100000;n++){
sum+=pow((-1/4),(n+1)) * pow((0.5-1),n);
}
printf("sum= %f\n",sum);
}

Problem right here:
sum+=pow((-1/4),(n+1)) * pow((0.5-1),n);
-1 is an integer literal, and so is 4; hence, (-1/4) is -0, and not -0.25 (which was probably what you wanted to use). Use floating point literals like -1.0 if you want them in C!

-1/4 will result to 0 as its an integer division, use floats instead:
(float)-1/4

1/4 refers to the euclidian division hence 0 obtained.
Use sum+=pow((-1.0/4.0),(n+1)) * pow((0.5-1),n); and you get the good results sum= -0.285714

Related

How to find empty space in a rectangle which has 2 circles in it?

I'm solving a problem in Toph . In this problem I've to find out the empty space of a rectangle which has 2 equal circles in it.
here is the problem
#include <stdio.h>
float pi=3.1416;
int main()
{
int i,t;
float r,rest;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%f",&r);
rest=(4*r*2*r)-(2*pi*r*r);
printf("Case %d: %.2f\n",i,rest);
}
return 0;
Here is my solve. It returns a correct value for first test case but it fails to solve the second one.
What's the problem???
float pi=3.1416; is the cause of the problem. Under the math header file (#include <math.h>) there is a constant M_PI use it instead.
Edit:
Sorry, didn't read thoroughly, apparently the problem is in the floating point precision. If you change all float values into double it should work.
#include <stdio.h>
double pi=3.1416;
int main()
{
int i,t;
double r,rest;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%lf",&r);
rest=(4*r*2*r)-(2*pi*r*r);
printf("Case %d: %.2lf\n",i,rest);
}
return 0;
}
Unlike 2 and 8, the reason double is more accurate is because float cannot represent 3.1416 as well as the input values:
3.1416 -> 3.1415998935699462890625
40.082 -> 40.082000732421875
85.8 -> 85.8000030517578125
There's simply not enough precision, (note that IEEE-754 float, which is overwhelmingly used for float, stores it in base-2.) Most probably, the later numbers were probably specifically generated in order to fail the test cases. If one wants to know more, Don’t Store That in a Float, and What Every Computer Scientist Should Know About Floating-Point Arithmetic.
The numerical constant is 1.7168, which is exact assuming their version of pi, (times r*r.) The best one could with single-point precision is 1.7167999744415283203125, which is off by 2.55584716796875E-8. With a double, it's 1.71680000000000010373923942097, off by 1.0373923942097E-16, plus the values input.

How to get repeating decimal in C

How can I get the repeating decimal 0.99999999... in C ?
1.0/9.0 will give me repeating decimal: 0.1111111, but if use
double = 1.0/9.0 * 9.0 The result is 1.0
Thank you.
The problem you are running into is while 1/9 is a repeating decimal both in base 10 (0.1111..) and in base 16, (0.1c71c7..) when you multiply by 9 again, you get 1... as far as the floating point routines are concerned. The way to get your 0.9999 in C is to break up the repeated decimal by subtracting just a little bit so it won't round back to 1. If you only want to see 9s, you will need to limit how much gets printed out by some means other than the floating point formatting. This will do what you need:
#include <stdio.h>
#include <string.h>
main()
{
double val=9;
char disp[21];
val=1.0/9.0;
val -= 0.0000000000000001;
printf("%20.19f\n", val);
val *=9;
printf("%20.19f\n", val);
sprintf(disp,"%20.19f",val);
printf("%.15s\n", disp);
}
This gives:
0.1111111111111110078
0.9999999999999991118
0.9999999999999
on my machine.
This happend, because results isn't really 0.1111111. You can check this with std::setprecision(numOfDecimals) from #include <iomanip>
Here is little example:
double a=1.0/9.0;
double b=9.0;
std::cout<<a*b; //result is 1
From mathematic reasons: (1/9)*(9/1)=(9/9)=1.
If we actualy have results like this:
double a=round(1.0/9.0 * 100.0)/100.0;//100 for 2 decimal places: here is 0.11 actualy
double b=9.0;
std::cout<<a*b; //result is 0.99
Example

To print the answer from the series -> x^3 - x^5 + x^7 - x^9 +

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,n;
float sum=0;
printf("Length and Value");
scanf("%d%d",&n,&x);
for(int i=1;i<=n;i++)
{
sum+=(pow(x,2*i+1) * pow(-1,i+1));
}
printf("%f",sum);
return 0;
}
I'm trying to solve this series in C language. Am I doing something wrong in the above code?
Yes, you're a bit wrong. In your code
printf("%f",sum);
sum is an int and using %f to print the value of an int is undefined behaviour.
The function pow() returns a double. You may want to change your sum to type double.
If you don't mind using your own version, a better looking implementation, without using pow() will be
Store the existing value.
Multiply by x * x on each iteration
Take care of -ve sign for even numbered iteration.
First things first, your printf has the wrong format specifier for an int: use %d instead. But for non-integral x, you'll want to refactor to a double anyway, so %f will probably be retained.
Secondly, don't use pow: it will not be precise (probably implemented as exp(log)) and you don't need to evaluate the power from scratch for each term.
Keep a running power: i.e. compute x * x * x initially, then successively multiply that by x * x for subsequent terms. Don't forget to alternate the signs: you can do that by multiplying by -1.
you are trying to find x^3,x^5 that is power in odd. so do a little change in your for loop. write this instead of your code. and if you give a large x or n value then it is preferable to declare sum as a long data type
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,n;
long sum=0;
printf("Length and Value");
scanf("%d%d",&n,&x);
for(int i=2;i<=n;i+=2)
{
sum+=(pow(x,i+1) * pow(-1,i));
}
printf("%l",sum);
return 0;
}
First of all, you are trying to evaluate a series that diverges for all points outside and on the circle of radius one (as a complex series). If you use an int for x, you will get each time values bigger and bigger, oscillating around 0. Try it with numbers of ||x|| < 1 (this means double or float for x)
All the other answers posted are also usefull to get sooner to the expected value.

Mathematical task with point numbers

I just started to study about C Programing.
I want to write a program to solve this mathematical task (1/2*r2*3.14)
This is the code that I wrote:
#include <stdio.h>
#include <conio.h>
main()
{
int r=5;
float sum;
sum = 1/2*r^2*3.14;
printf("%f", sum);
getch();
}
but there is an error and I don`t know what the mistake is.
First I thought that there is something wrong about the number 3.14, but when I changed it to 3 the program ran but the answer was 6.0000 but it should be 37.5
In C there is no operator for power calculation. ^ is used as XOR operator. You need to use library function pow for power calculations.
sum = 1.0 / 2 * pow(r,2) * 3.14;
Note that I changed 1/2 to 1.0/2 because 1/2 will always give 0 and the result you will get is 0.
^ is bitwise XOR operator. You have to use pow() for your purpose
sum = 1.0/2.0*pow(r,2)*3.14;
Your code will give you 6.000. Because ^ is using as xor operator
1/2*r^2*3 = (0)d ^ (6)d = (000)b ^ (110)b = (110)b = (6)d
But, 3.14 instead of 3 will give you error
1/2*r^2*3.14
Because, Xor operator don't take double as operand
The ^ operator is not for power raising you can write it explicitly
#include <stdio.h>
#include <conio.h>
main()
{
int r=5;
float sum;
/* 1/2 * r^2 * pi <- this is the expression */
sum = 1.57 * r * r;
printf("%f", sum);
getch();
}
the expression has no meaning in the program except for someone reading it, so you can add a comment and write the values directly.
And in case you are going to raise to a higher power just use the pow() function.
Also, if you skip the .0 the compiler assumes the values as integers, and 1/2 is 0.5 truncated it yeilds 0, 1./2 would also work, but not 1/2.

Multiplication of float with int in c

#include <stdio.h>
int main(){
float var = 0.612;
printf("%f\n",var);
printf("%f\n",var*100);
return 0;
}
o/p
0.612000
61.199997
I found that for JavaScript, we have .tofixed() method.
How do we get a fix for this in C?
You can specify the precision when printing:
printf("%.3f\n", 100 * var);
Since the exact number you're having probably isn't representable in the float itself, there is no operation you can do on the number itself to "remove" the decimals, it's all a matter of how you choose to present the data.

Resources