Calculating a Serie in C
This image is the task I should do:
Whatever I enter between -1 and 1, the outputs are always 1.0000 or 2.0000. How can I do solve this problem? Below I attached my code.
#include <stdio.h>
#include <math.h>
int main() {
int i;
float x;
float sum=0;
printf ("enter an xn");
scanf ("%f",&x);
if ((x>-1)&&(x<1))
{
for (i=0;i<101;i++)
sum= sum + (pow(x,i));
}
printf ("result=%f",sum);
return 0;
}
c series
add a comment |
This image is the task I should do:
Whatever I enter between -1 and 1, the outputs are always 1.0000 or 2.0000. How can I do solve this problem? Below I attached my code.
#include <stdio.h>
#include <math.h>
int main() {
int i;
float x;
float sum=0;
printf ("enter an xn");
scanf ("%f",&x);
if ((x>-1)&&(x<1))
{
for (i=0;i<101;i++)
sum= sum + (pow(x,i));
}
printf ("result=%f",sum);
return 0;
}
c series
1
"But my code is not working." That's not a good description of our problem. What is not working? Which output do you expect? Which output do you get?
– M Oehm
Nov 20 at 21:20
you need a float forx
but youscanf
anint
– deamentiaemundi
Nov 20 at 21:24
if you are not planning to use integer please change the format of X to float or double and change the" scanf " function like this scanf("%f", &x); or change your if ((x>-1) && (x<1)) statements. Only valid integer is 0 in this scenario
– Cagri Candan
Nov 20 at 21:29
i changed x to float but it still doesn`t give the right answer @deamentiaemundi @Cagri Candan
– noob
Nov 20 at 21:54
1
@noob that's just caused by loss of precision. Withx = 0.5
andn=100
that series gives you 1.9999999999999999999999999999992111390947789881945882714347172137703267935648909769952297210693359375 which is outside of the precision of the typefloat
. Just try with a smallern
sayn = 10
or with a much smallerx
sayx = 0.001
– deamentiaemundi
Nov 21 at 1:06
add a comment |
This image is the task I should do:
Whatever I enter between -1 and 1, the outputs are always 1.0000 or 2.0000. How can I do solve this problem? Below I attached my code.
#include <stdio.h>
#include <math.h>
int main() {
int i;
float x;
float sum=0;
printf ("enter an xn");
scanf ("%f",&x);
if ((x>-1)&&(x<1))
{
for (i=0;i<101;i++)
sum= sum + (pow(x,i));
}
printf ("result=%f",sum);
return 0;
}
c series
This image is the task I should do:
Whatever I enter between -1 and 1, the outputs are always 1.0000 or 2.0000. How can I do solve this problem? Below I attached my code.
#include <stdio.h>
#include <math.h>
int main() {
int i;
float x;
float sum=0;
printf ("enter an xn");
scanf ("%f",&x);
if ((x>-1)&&(x<1))
{
for (i=0;i<101;i++)
sum= sum + (pow(x,i));
}
printf ("result=%f",sum);
return 0;
}
c series
c series
edited Nov 21 at 12:01
kit
1,1083616
1,1083616
asked Nov 20 at 21:06
noob
82
82
1
"But my code is not working." That's not a good description of our problem. What is not working? Which output do you expect? Which output do you get?
– M Oehm
Nov 20 at 21:20
you need a float forx
but youscanf
anint
– deamentiaemundi
Nov 20 at 21:24
if you are not planning to use integer please change the format of X to float or double and change the" scanf " function like this scanf("%f", &x); or change your if ((x>-1) && (x<1)) statements. Only valid integer is 0 in this scenario
– Cagri Candan
Nov 20 at 21:29
i changed x to float but it still doesn`t give the right answer @deamentiaemundi @Cagri Candan
– noob
Nov 20 at 21:54
1
@noob that's just caused by loss of precision. Withx = 0.5
andn=100
that series gives you 1.9999999999999999999999999999992111390947789881945882714347172137703267935648909769952297210693359375 which is outside of the precision of the typefloat
. Just try with a smallern
sayn = 10
or with a much smallerx
sayx = 0.001
– deamentiaemundi
Nov 21 at 1:06
add a comment |
1
"But my code is not working." That's not a good description of our problem. What is not working? Which output do you expect? Which output do you get?
– M Oehm
Nov 20 at 21:20
you need a float forx
but youscanf
anint
– deamentiaemundi
Nov 20 at 21:24
if you are not planning to use integer please change the format of X to float or double and change the" scanf " function like this scanf("%f", &x); or change your if ((x>-1) && (x<1)) statements. Only valid integer is 0 in this scenario
– Cagri Candan
Nov 20 at 21:29
i changed x to float but it still doesn`t give the right answer @deamentiaemundi @Cagri Candan
– noob
Nov 20 at 21:54
1
@noob that's just caused by loss of precision. Withx = 0.5
andn=100
that series gives you 1.9999999999999999999999999999992111390947789881945882714347172137703267935648909769952297210693359375 which is outside of the precision of the typefloat
. Just try with a smallern
sayn = 10
or with a much smallerx
sayx = 0.001
– deamentiaemundi
Nov 21 at 1:06
1
1
"But my code is not working." That's not a good description of our problem. What is not working? Which output do you expect? Which output do you get?
– M Oehm
Nov 20 at 21:20
"But my code is not working." That's not a good description of our problem. What is not working? Which output do you expect? Which output do you get?
– M Oehm
Nov 20 at 21:20
you need a float for
x
but you scanf
an int
– deamentiaemundi
Nov 20 at 21:24
you need a float for
x
but you scanf
an int
– deamentiaemundi
Nov 20 at 21:24
if you are not planning to use integer please change the format of X to float or double and change the" scanf " function like this scanf("%f", &x); or change your if ((x>-1) && (x<1)) statements. Only valid integer is 0 in this scenario
– Cagri Candan
Nov 20 at 21:29
if you are not planning to use integer please change the format of X to float or double and change the" scanf " function like this scanf("%f", &x); or change your if ((x>-1) && (x<1)) statements. Only valid integer is 0 in this scenario
– Cagri Candan
Nov 20 at 21:29
i changed x to float but it still doesn`t give the right answer @deamentiaemundi @Cagri Candan
– noob
Nov 20 at 21:54
i changed x to float but it still doesn`t give the right answer @deamentiaemundi @Cagri Candan
– noob
Nov 20 at 21:54
1
1
@noob that's just caused by loss of precision. With
x = 0.5
and n=100
that series gives you 1.9999999999999999999999999999992111390947789881945882714347172137703267935648909769952297210693359375 which is outside of the precision of the type float
. Just try with a smaller n
say n = 10
or with a much smaller x
say x = 0.001
– deamentiaemundi
Nov 21 at 1:06
@noob that's just caused by loss of precision. With
x = 0.5
and n=100
that series gives you 1.9999999999999999999999999999992111390947789881945882714347172137703267935648909769952297210693359375 which is outside of the precision of the type float
. Just try with a smaller n
say n = 10
or with a much smaller x
say x = 0.001
– deamentiaemundi
Nov 21 at 1:06
add a comment |
2 Answers
2
active
oldest
votes
Even using a type like double
you haven't enough numerical precision to sum all the powers up to 100.
Executing the following snippet, you'll notice that, while the corret (numerically speaking) result is evaluated, the loop stops way before the 100th iteration, tipically at 16:
#include <stdio.h>
#include <math.h>
#include <float.h>
// Analitically calculates the limit for n -> inf of the series of powers
double sum_of_powers_limit(double x)
{
return 1.0 / (1.0 - x);
}
int main(void)
{
double x = 0.1;
const int N = 100;
double sum = 1.0;
for (int i = 1; i <= N; ++i)
{
double old_sum = sum;
sum = sum + pow(x,i);
if (old_sum == sum)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
printf(" result = %.*en", DBL_DECIMAL_DIG, sum);
printf("expected = %.*en", DBL_DECIMAL_DIG, sum_of_powers_limit(x));
return 0;
}
Also note that a more efficient way to evaluate this kind of polynomials is the Horner's method:
// Evaluates the sum s(x) = 1 + x + x^2 + ... + x^n using Horner's method
// It stops when it cannot update the value anymore
double sum_of_powers(double x, int n)
{
double result = 1.0;
for (int i = 0; i < n; ++i)
{
double old_result = result;
result = 1.0 + x * result;
if (old_result == result)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
return result;
}
add a comment |
if ((x>-1)&&(x<1))
With this case your code will work only if x is zero so try removing if statement and do mention what output you expect for given particular input, it will be bit more helpful to answer it.
Try this code:
#include <stdio.h>
#include <math.h>
int main() {
int i; float x;
float sum=0;
printf ("enter an xn");
scanf ("%f",&x);
for (i=0 ;i<101; i++)
sum+= (pow(x,i));
printf ("result=%f",sum);
return 0;
}
x had to be a float!
– Mike
Nov 21 at 6:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401540%2fcalculating-a-serie-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Even using a type like double
you haven't enough numerical precision to sum all the powers up to 100.
Executing the following snippet, you'll notice that, while the corret (numerically speaking) result is evaluated, the loop stops way before the 100th iteration, tipically at 16:
#include <stdio.h>
#include <math.h>
#include <float.h>
// Analitically calculates the limit for n -> inf of the series of powers
double sum_of_powers_limit(double x)
{
return 1.0 / (1.0 - x);
}
int main(void)
{
double x = 0.1;
const int N = 100;
double sum = 1.0;
for (int i = 1; i <= N; ++i)
{
double old_sum = sum;
sum = sum + pow(x,i);
if (old_sum == sum)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
printf(" result = %.*en", DBL_DECIMAL_DIG, sum);
printf("expected = %.*en", DBL_DECIMAL_DIG, sum_of_powers_limit(x));
return 0;
}
Also note that a more efficient way to evaluate this kind of polynomials is the Horner's method:
// Evaluates the sum s(x) = 1 + x + x^2 + ... + x^n using Horner's method
// It stops when it cannot update the value anymore
double sum_of_powers(double x, int n)
{
double result = 1.0;
for (int i = 0; i < n; ++i)
{
double old_result = result;
result = 1.0 + x * result;
if (old_result == result)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
return result;
}
add a comment |
Even using a type like double
you haven't enough numerical precision to sum all the powers up to 100.
Executing the following snippet, you'll notice that, while the corret (numerically speaking) result is evaluated, the loop stops way before the 100th iteration, tipically at 16:
#include <stdio.h>
#include <math.h>
#include <float.h>
// Analitically calculates the limit for n -> inf of the series of powers
double sum_of_powers_limit(double x)
{
return 1.0 / (1.0 - x);
}
int main(void)
{
double x = 0.1;
const int N = 100;
double sum = 1.0;
for (int i = 1; i <= N; ++i)
{
double old_sum = sum;
sum = sum + pow(x,i);
if (old_sum == sum)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
printf(" result = %.*en", DBL_DECIMAL_DIG, sum);
printf("expected = %.*en", DBL_DECIMAL_DIG, sum_of_powers_limit(x));
return 0;
}
Also note that a more efficient way to evaluate this kind of polynomials is the Horner's method:
// Evaluates the sum s(x) = 1 + x + x^2 + ... + x^n using Horner's method
// It stops when it cannot update the value anymore
double sum_of_powers(double x, int n)
{
double result = 1.0;
for (int i = 0; i < n; ++i)
{
double old_result = result;
result = 1.0 + x * result;
if (old_result == result)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
return result;
}
add a comment |
Even using a type like double
you haven't enough numerical precision to sum all the powers up to 100.
Executing the following snippet, you'll notice that, while the corret (numerically speaking) result is evaluated, the loop stops way before the 100th iteration, tipically at 16:
#include <stdio.h>
#include <math.h>
#include <float.h>
// Analitically calculates the limit for n -> inf of the series of powers
double sum_of_powers_limit(double x)
{
return 1.0 / (1.0 - x);
}
int main(void)
{
double x = 0.1;
const int N = 100;
double sum = 1.0;
for (int i = 1; i <= N; ++i)
{
double old_sum = sum;
sum = sum + pow(x,i);
if (old_sum == sum)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
printf(" result = %.*en", DBL_DECIMAL_DIG, sum);
printf("expected = %.*en", DBL_DECIMAL_DIG, sum_of_powers_limit(x));
return 0;
}
Also note that a more efficient way to evaluate this kind of polynomials is the Horner's method:
// Evaluates the sum s(x) = 1 + x + x^2 + ... + x^n using Horner's method
// It stops when it cannot update the value anymore
double sum_of_powers(double x, int n)
{
double result = 1.0;
for (int i = 0; i < n; ++i)
{
double old_result = result;
result = 1.0 + x * result;
if (old_result == result)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
return result;
}
Even using a type like double
you haven't enough numerical precision to sum all the powers up to 100.
Executing the following snippet, you'll notice that, while the corret (numerically speaking) result is evaluated, the loop stops way before the 100th iteration, tipically at 16:
#include <stdio.h>
#include <math.h>
#include <float.h>
// Analitically calculates the limit for n -> inf of the series of powers
double sum_of_powers_limit(double x)
{
return 1.0 / (1.0 - x);
}
int main(void)
{
double x = 0.1;
const int N = 100;
double sum = 1.0;
for (int i = 1; i <= N; ++i)
{
double old_sum = sum;
sum = sum + pow(x,i);
if (old_sum == sum)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
printf(" result = %.*en", DBL_DECIMAL_DIG, sum);
printf("expected = %.*en", DBL_DECIMAL_DIG, sum_of_powers_limit(x));
return 0;
}
Also note that a more efficient way to evaluate this kind of polynomials is the Horner's method:
// Evaluates the sum s(x) = 1 + x + x^2 + ... + x^n using Horner's method
// It stops when it cannot update the value anymore
double sum_of_powers(double x, int n)
{
double result = 1.0;
for (int i = 0; i < n; ++i)
{
double old_result = result;
result = 1.0 + x * result;
if (old_result == result)
{
fprintf(stderr, "Numerical precision limit reached at i = %dn", i);
break;
}
}
return result;
}
answered Nov 21 at 10:15
Bob__
4,70331425
4,70331425
add a comment |
add a comment |
if ((x>-1)&&(x<1))
With this case your code will work only if x is zero so try removing if statement and do mention what output you expect for given particular input, it will be bit more helpful to answer it.
Try this code:
#include <stdio.h>
#include <math.h>
int main() {
int i; float x;
float sum=0;
printf ("enter an xn");
scanf ("%f",&x);
for (i=0 ;i<101; i++)
sum+= (pow(x,i));
printf ("result=%f",sum);
return 0;
}
x had to be a float!
– Mike
Nov 21 at 6:55
add a comment |
if ((x>-1)&&(x<1))
With this case your code will work only if x is zero so try removing if statement and do mention what output you expect for given particular input, it will be bit more helpful to answer it.
Try this code:
#include <stdio.h>
#include <math.h>
int main() {
int i; float x;
float sum=0;
printf ("enter an xn");
scanf ("%f",&x);
for (i=0 ;i<101; i++)
sum+= (pow(x,i));
printf ("result=%f",sum);
return 0;
}
x had to be a float!
– Mike
Nov 21 at 6:55
add a comment |
if ((x>-1)&&(x<1))
With this case your code will work only if x is zero so try removing if statement and do mention what output you expect for given particular input, it will be bit more helpful to answer it.
Try this code:
#include <stdio.h>
#include <math.h>
int main() {
int i; float x;
float sum=0;
printf ("enter an xn");
scanf ("%f",&x);
for (i=0 ;i<101; i++)
sum+= (pow(x,i));
printf ("result=%f",sum);
return 0;
}
if ((x>-1)&&(x<1))
With this case your code will work only if x is zero so try removing if statement and do mention what output you expect for given particular input, it will be bit more helpful to answer it.
Try this code:
#include <stdio.h>
#include <math.h>
int main() {
int i; float x;
float sum=0;
printf ("enter an xn");
scanf ("%f",&x);
for (i=0 ;i<101; i++)
sum+= (pow(x,i));
printf ("result=%f",sum);
return 0;
}
edited Nov 21 at 11:02
answered Nov 20 at 21:29
Ketan Ramteke
666
666
x had to be a float!
– Mike
Nov 21 at 6:55
add a comment |
x had to be a float!
– Mike
Nov 21 at 6:55
x had to be a float!
– Mike
Nov 21 at 6:55
x had to be a float!
– Mike
Nov 21 at 6:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401540%2fcalculating-a-serie-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"But my code is not working." That's not a good description of our problem. What is not working? Which output do you expect? Which output do you get?
– M Oehm
Nov 20 at 21:20
you need a float for
x
but youscanf
anint
– deamentiaemundi
Nov 20 at 21:24
if you are not planning to use integer please change the format of X to float or double and change the" scanf " function like this scanf("%f", &x); or change your if ((x>-1) && (x<1)) statements. Only valid integer is 0 in this scenario
– Cagri Candan
Nov 20 at 21:29
i changed x to float but it still doesn`t give the right answer @deamentiaemundi @Cagri Candan
– noob
Nov 20 at 21:54
1
@noob that's just caused by loss of precision. With
x = 0.5
andn=100
that series gives you 1.9999999999999999999999999999992111390947789881945882714347172137703267935648909769952297210693359375 which is outside of the precision of the typefloat
. Just try with a smallern
sayn = 10
or with a much smallerx
sayx = 0.001
– deamentiaemundi
Nov 21 at 1:06