Home > Back-end >  Rich to the poor problem why my result finally with no decimal to see a lot of similar online withou
Rich to the poor problem why my result finally with no decimal to see a lot of similar online withou

Time:10-09

    

# include & lt; Stdio. H>
# include & lt; Math. H>
# define W 1 e5
# define F 1 e - 2
Int main (void)
{
Float sum1=0, sum2=0;
Int I, j=1;
For (I=1; I & lt;=30; I++)
{
Sum1=sum1 + W;
Sum2=sum2 + F * j;
J=j * 2.0;
Printf (" % f \ n ", sum2);
}
Printf (" to Stranger: % 2 f yuan \ n ", sum2);
Printf (" to Richman: % 2 f yuan \ n ", sum1);
return 0;
}

CodePudding user response:

Liuxinjia119 @ liuxinjia119: ~ $vi 1 c
Liuxinjia119 @ liuxinjia119: ~ $GCC -o 1. Out 1 c
Liuxinjia119 @ liuxinjia119: ~ $./1. Out
0.010000
0.030000
0.070000
0.150000
0.310000
0.630000
1.270000
2.550000
5.110000
10.230000
20.470001
40.950001
81.910004
163.830002
327.670013
655.350037
1310.710083
2621.430176
5242.870117
10485.750000
20971.509766
41943.031250
83886.070312
167772.156250
335544.312500
671088.625000
1342177.250000
2684354.500000
5368709.000000
10737418.000000
The to Stranger: 10737418.00 yuan
The to Richman: 3000000.00 yuan
Results with decimal ah, this is the result of my input in vim

CodePudding user response:

Don't want a decimal point those 0, can be used to replace % % g f output when the printf

Default is % f output decimal point 6, % g can put 0.100000 this output into % 0.1

CodePudding user response:

reference 1st floor liu2002119 response:
liuxinjia119 @ liuxinjia119: ~ $vi 1 c
Liuxinjia119 @ liuxinjia119: ~ $GCC -o 1. Out 1 c
Liuxinjia119 @ liuxinjia119: ~ $./1. Out
0.010000
0.030000
0.070000
0.150000
0.310000
0.630000
1.270000
2.550000
5.110000
10.230000
20.470001
40.950001
81.910004
163.830002
327.670013
655.350037
1310.710083
2621.430176
5242.870117
10485.750000
20971.509766
41943.031250
83886.070312
167772.156250
335544.312500
671088.625000
1342177.250000
2684354.500000
5368709.000000
10737418.000000
The to Stranger: 10737418.00 yuan
The to Richman: 3000000.00 yuan
Results with decimal ah, this is the result of my input in vim

Sorry I didn't clearly stated, is the answer to the final result is a no. 23, 000,
And macro definition here F decimal only to the second,
So why some results among
In the sixth decimal places are null
(or second decimal point should be fill in back position should not appear not null)

CodePudding user response:

Sum1 and sum2 with double wouldn't be so,
  • Related