about output of floating point numbers

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

about output of floating point numbers

Post by 10153EN »

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?
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

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?
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN »

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
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

That's for problem 453:

#define norm(a) (((a)>=-5E-4 && (a)<=5E-4) ? 0.0 : (a))
void show ( double x, double y ) {
printf("(%.3f,%.3f)", norm(x), norm(y));
}
Moni
Experienced poster
Posts: 202
Joined: Fri Mar 22, 2002 2:00 am
Location: Chittagong. CSE - CUET
Contact:

Post by Moni »

Sorry this was submitted with mistakes. :oops:
Last edited by Moni on Wed Jul 03, 2002 8:40 pm, edited 1 time in total.
ImageWe are all in a circular way, no advances, only moving and moving!
Moni
Experienced poster
Posts: 202
Joined: Fri Mar 22, 2002 2:00 am
Location: Chittagong. CSE - CUET
Contact:

Post by Moni »

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)

  • Float 32 3.4e+38 6
    Double 64 1.7e+308 15
    Long Double 64 1.7e+308 15
[/color]
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
[/color]
Here we can see that long double is really very good data type for handling huge data but if I use the integer:
11111111111111111111111111111111111111111111111111111111111111111
In long double or double data type the value is totally changed! why it happen? :(
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? :cry:
And the last thing, if I want to figure-out 20 or 30 digits accurately for the value of ''pi'' what can I do?
:roll:
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. :-?
ImageWe are all in a circular way, no advances, only moving and moving!
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

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)
Daniel Chia
New poster
Posts: 12
Joined: Mon Jul 29, 2002 3:04 pm
Contact:

Post by Daniel Chia »

not sure how accurate this is, but u can get pi by using
[c]
2*acos(0);
[/c]

My -1 cents.
Post Reply

Return to “C”