Home > Back-end >  Freshman beginner C, youdao problem don't understand about fabs
Freshman beginner C, youdao problem don't understand about fabs

Time:10-02

Topic as shown in figure
The correct answer is: # include "stdio.h"

# include "math. H"

Void main ()

{

Double s=0, tn=1, I=1;

Double sign=1;

While (fabs (tn) & gt; E=1-4)

{

Tn=sign/I;

S=s + tn;

i++;

Sign=- sign;

}

Printf (" % s=f \ n ", s);

}
The result is 0.693197

I'm useless fabs, I practice the following # include
Void main (void)
{
Int I=1, n=1;
Double sum=0, t=1;
While (1.0/i> E=1-4)
{
T=n * 1.0/I;
Sum=sum + t;
I=I + 1;
N=- n;
}
Printf (" s=% f ", sum);

}
Result is 0.693097, and why, meaning is the same, is the last item on the absolute value is less than 1 e - 4, why value and use fabs different?

CodePudding user response:

There is something wrong with the question understanding
Title means the absolute value of the last item is less than 1 e - 4 (participate in operation, is the last one)
You don't have the combined with the end product

CodePudding user response:

reference 1st floor m0_44977681 response:
there is something wrong with the question understanding
Title means the absolute value of the last item is less than 1 e - 4 (participate in operation, is the last one)
You didn't work out the final the plus

I points is one of the last item, regardless of the initial value is 1, I i++, I always be positive

CodePudding user response:

reference 1st floor m0_44977681 response:
there is something wrong with the question understanding
Title means the absolute value of the last item is less than 1 e - 4 (participate in operation, is the last one)
You didn't work out the final the plus

I points is one of the last item, the initial value is 1, I i++, I always be positive, the absolute value of the last item is not one I points

CodePudding user response:

The last item is one of the symbols item by I points, initial value is 1, I i++, I always be positive, the absolute value of the last item is not one I points

CodePudding user response:

Why don't you change the int to double to try again

CodePudding user response:

reference 5 floor focuslight reply:
why don't you change the int to double try again

Or 0.693097

CodePudding user response:

You are right, the answer wrong

Analyze the following
 # include "math. H" 
Void main ()
{
Double s=0, tn=1, I=1;
Double sign=1;

While (fabs (tn) & gt; E=1-4) didn't update//step 4 tn at this time, or 0.0001
{
Tn=sign/I;/* this time step 5 is equal to 10001, I finally tn boundaries, but the process is to continue, then added a 1.0/10001 */
/* step 1. Assuming that I have is equal to 10000, this time is the cut-off point
Tn just at this time is equal to 1-4 e (0.0001), go down */
S=s + tn;/* step 2:0.0001 has been added, they cannot enter the cycle again, but,,,, */

i++; Step 3:/* I have equal to 10001.0 */
Sign=- sign;
}

Printf (" % s=f \ n ", s);

}
/*
Although we have analysis is clear, but you see, at the beginning of the cycle is nothing wrong with

In fact there is a problem, the problem is that cycle control conditions tn
Tn in the loop body, has been with the while (tn?? ) do not match the value when in the cycle control: by (tn=sign/I) change
But he continued to participate in the operation, and change the values of other variables, yapping ~ ~ ~ ~ ~ ~ here omit 500 words, thus violating the designed

Talk about the precision of the floating-point
You answered the questions in the print function, and your own print function change
To printf (" % s=20 f \ n ", s); What, you will find? The value is not poor that 0.0001 but poor 1.0/10001

Leave a problem for you to think again see
Double f1=0.1
Float f2=0.1
If (==f1 f2)??????

*/

CodePudding user response:

The
reference 2 floor? AraVio response:
Quote: refer to 1st floor m0_44977681 response:
there is something wrong with the question understanding
Title means the absolute value of the last item is less than 1 e - 4 (participate in operation, is the last one)
You didn't work out the final the plus

I points is one of the last item, regardless of the initial value is 1, I i++, I always be positive

I one of the points is the absolute value of the last item on the

But the original meaning is the last item on the absolute value of less than 1 e - 4
Inside the absolute value of each item you are more than 1 e - 4, did not put the last absolute value less than 1-4 e added that a
Look at the topic "until the last item on the absolute value is less than 1 e - 4 (so far)", understand not
You put your inside while I I - 1 instead of the

The answer is to create a tn before i++ assignment, that is to say again after the end of this cycle to judge the time tn is the absolute value of the recycled items, if the use items that meet the conditions, then do it again circulation, relative to you run one more item (that is, when the absolute value of the operation of less than 1 e - 4), is your incoming i++ after 1/I the next cycle is going to use the absolute value of the item, added a few

Is you add a less, is the absolute value of less than 1-4 e that a
, your item is the absolute value is greater than 1 e - 4, see

CodePudding user response:

Directly in the while I change to I - 1 is not rigorous, because the initial value is 1, I I - 1 do the denominator is not strict, so the first step in either the operations to be done, or consider using the answer that way

CodePudding user response:

references 9 f m0_44977681 response:
directly in the while I change to I - 1 is not rigorous, because the initial value is 1, I I - 1 do the denominator is not rigorous, so the first step in either the operations to be done, or consider using the answer that way

Double sum=1, I=2, sign=1;
While (1/(I - 1) & gt; E=1-4)
{
Sum=sum + sign/I;
i++;
Sign=- sign;
}

CodePudding user response:

refer to the eighth floor m0_44977681 response:
Quote: refer to the second floor? AraVio response:
Quote: refer to 1st floor m0_44977681 response:
there is something wrong with the question understanding
Title means the absolute value of the last item is less than 1 e - 4 (participate in operation, is the last one)
You didn't work out the final the plus

I points is one of the last item, regardless of the initial value is 1, I i++, I always be positive

I one of the points is the absolute value of the last item on the

But the original meaning is the last item on the absolute value of less than 1 e - 4
Inside the absolute value of each item you are more than 1 e - 4, did not put the last absolute value less than 1-4 e added that a
Look at the topic "until the last item on the absolute value is less than 1 e - 4 (so far)", understand not
You put your inside while I I - 1 instead of the

The answer is to create a tn before i++ assignment, that is to say again after the end of this cycle to judge the time tn is the absolute value of the recycled items, if the use items that meet the conditions, then do it again circulation, relative to you run one more item (that is, when the absolute value of the operation of less than 1 e - 4), is your incoming i++ after 1/I the next cycle is going to use the absolute value of the item, added a few

Is you add a less, is the absolute value of less than 1-4 e that a
, your item is the absolute value is greater than 1 e - 4, see

I feel the 7th floor said is right

CodePudding user response:

refer to the eighth floor m0_44977681 response:
Quote: refer to the second floor? nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related