I usually faced precision error when solving some problems involving floating point number, and so I here wanna ask for some dear advice.
Firstly, I wanna ask is there any difference between float and double, except their range of range of representation? Since from some threads some programmers stated that to use float but not to use double so as to got accepted for a problem.
Secondly, I wanna what's a good, or a reliable way to fulfill the requirement of precision output, e.g. rounded to 3 decimal digits?
I know that using
printf("%.3lf\n", variable) is not reliable enough.
Would anyone give me a hand?
about output of floating point numbers
Moderator: Board moderators
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
doubles usually have 64 bits, floats have 32. They're splitted 1/11/52 and 1/8/23 respectively for signbit/exponent/mantissa, where exponent is the main contributor for range and mantissa for precision.
You can see that doubles have a larger range and better precision. The only reason to suggest using floats is when the problem writer used floats and you want to produce the same bad rounding errors to get the same bad results as the judge.
I always use the printf method (without the "l", though) you mentioned. I think only once did I have to fix it because it printed "-0.000". Why do you think it is not reliable enough?
You can see that doubles have a larger range and better precision. The only reason to suggest using floats is when the problem writer used floats and you want to produce the same bad rounding errors to get the same bad results as the judge.
I always use the printf method (without the "l", though) you mentioned. I think only once did I have to fix it because it printed "-0.000". Why do you think it is not reliable enough?
The reason why I thought using the printf("%lf\n", variable) (or without "l" =p) is not reliable enough is that I am not good at solving problems involving floating point numbers. I saw that in the ranklist of some problems, some programmers leave a message that "be careful of precision error". And I remembered, quite unforgettably, that in solving one of the problems, my program got accepted after being modified so as to reduce the floating point error.
Thus, I am quite afraid of trying to solve such problems (i mean the problems involving floating point numbers), although the problem seems easy to be solved.
By the way, could you mind sharing your technique employed in handling the -0.000 case? Thx
Thus, I am quite afraid of trying to solve such problems (i mean the problems involving floating point numbers), although the problem seems easy to be solved.
By the way, could you mind sharing your technique employed in handling the -0.000 case? Thx
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- Experienced poster
- Posts: 202
- Joined: Fri Mar 22, 2002 2:00 am
- Location: Chittagong. CSE - CUET
- Contact:
Hi,
Here two very renowned posters are consulting. I do not want to express any opinion here but I want to know from both of you that how can I get most accuracy in using ''float'' and ''double'' for only rounding to 3 decimal digits and for 20 or more decimal digits.
According to a book for 32-bit UNIX machine:
Data type Bits Highest Value Accuracy (digits)
And for BC++ or TC++ or most other 16-bit machines:
Here we can see that long double is really very good data type for handling huge data but if I use the integer:
If I got any long double or double data which is basically an integer but it may be 20 digits long and I use the modulus operator to get the last two or three digits I do not get the real value, why?
And the last thing, if I want to figure-out 20 or 30 digits accurately for the value of ''pi'' what can I do?
Form my point of view these are very important questions not only for the contest or problem solving but also for data structure and software development. So, on behalf of many programmers I really want a very good reply.

Here two very renowned posters are consulting. I do not want to express any opinion here but I want to know from both of you that how can I get most accuracy in using ''float'' and ''double'' for only rounding to 3 decimal digits and for 20 or more decimal digits.
According to a book for 32-bit UNIX machine:
Data type Bits Highest Value Accuracy (digits)
- Float 32 3.4e+38 6
Double 64 1.7e+308 15
Long Double 64 1.7e+308 15
And for BC++ or TC++ or most other 16-bit machines:
- Float 32 3.4e+38 6
Double 64 1.7e+308 15
Long Double 80 3.4e+4932 17
Here we can see that long double is really very good data type for handling huge data but if I use the integer:
In long double or double data type the value is totally changed! why it happen?11111111111111111111111111111111111111111111111111111111111111111

If I got any long double or double data which is basically an integer but it may be 20 digits long and I use the modulus operator to get the last two or three digits I do not get the real value, why?

And the last thing, if I want to figure-out 20 or 30 digits accurately for the value of ''pi'' what can I do?

Form my point of view these are very important questions not only for the contest or problem solving but also for data structure and software development. So, on behalf of many programmers I really want a very good reply.


-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
80 bits are about 24 decimal digits. Your number has 66. I'm not surprised at all that you lose precision.
I'm not sure if the modulo operator works on doubles&Co. There's the function
double fmod(double x, double y);
but maybe they just added it for other reasons. Really don't know...
You can get Pi on the internet. Search Google for this:
"3.1415926535" "million digits"
(i.e. think big if you want something)
I'm not sure if the modulo operator works on doubles&Co. There's the function
double fmod(double x, double y);
but maybe they just added it for other reasons. Really don't know...
You can get Pi on the internet. Search Google for this:
"3.1415926535" "million digits"
(i.e. think big if you want something)
-
- New poster
- Posts: 12
- Joined: Mon Jul 29, 2002 3:04 pm
- Contact: