C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Formatting numbers nicely with DBL_EPSILON

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jodawi
Guest





PostPosted: Fri Nov 12, 2004 4:04 am    Post subject: Formatting numbers nicely with DBL_EPSILON Reply with quote



It's common for values like 6.0 to turn into values like
5.9999999999999991 when doing floating point calculations. What's the
best way to display these to users as nice numbers? I've been using
DBL_EPSILON to nudge numbers for formatting purposes for a while now
(see example below).

It seems to work (although I haven't tested the particular code
included below), but I always have a nagging feeling that there might
be a better way. Or that multiplying by (1.0 + DBL_EPSILON) may not
always change the value, so some repeating values may be missed. Any
thoughts?


ostringstream formattedString;
formattedString << fixed;
// Round .xxx99999999 up by adding a very small number
x *= (1.0 + DBL_EPSILON);

formattedString << setprecision(f.digits);
formattedString << x;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Falk Tannhäuser
Guest





PostPosted: Sun Nov 14, 2004 4:56 pm    Post subject: Re: Formatting numbers nicely with DBL_EPSILON Reply with quote



Jodawi wrote:
Quote:
It's common for values like 6.0 to turn into values like
5.9999999999999991 when doing floating point calculations. What's the
best way to display these to users as nice numbers? I've been using
DBL_EPSILON to nudge numbers for formatting purposes for a while now
(see example below).
[...]
ostringstream formattedString;
formattedString << fixed;
// Round .xxx99999999 up by adding a very small number
x *= (1.0 + DBL_EPSILON);

formattedString << setprecision(f.digits);
formattedString << x;

How can you be sure that this will not turn 6.0 into
6.00000000000000178? It does on my implementation,
when I set the precision to 18.

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Sun Nov 14, 2004 4:56 pm    Post subject: Re: Formatting numbers nicely with DBL_EPSILON Reply with quote



Jodawi <jodawi (AT) gmail (DOT) com> wrote:

Quote:
It's common for values like 6.0 to turn into values like
5.9999999999999991 when doing floating point calculations. What's the
best way to display these to users as nice numbers? I've been using
DBL_EPSILON to nudge numbers for formatting purposes for a while now
(see example below).

It seems to work (although I haven't tested the particular code
included below), but I always have a nagging feeling that there might
be a better way. Or that multiplying by (1.0 + DBL_EPSILON) may not
always change the value, so some repeating values may be missed. Any
thoughts?


ostringstream formattedString;
formattedString << fixed;
// Round .xxx99999999 up by adding a very small number
x *= (1.0 + DBL_EPSILON);
Its not going to work in general since the expression

(1.+DBL_EPSILON) == 1. , evaluates to true. so except for errors in
the numeric value of DBL_EPSILON [from binary to decimal conversion,
since on most modern hardware, DBL_EPSILON in the internal format is an
exact number. Write the number to an ostringstream using the formating
manipulators currently used on the output stream and then
examine the resulting string for a trailing sequence of '9's and modify
the string. Then output it.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jodawi
Guest





PostPosted: Mon Nov 15, 2004 11:36 am    Post subject: Re: Formatting numbers nicely with DBL_EPSILON Reply with quote

Falk Tannhäuser <clcppm-poster (AT) this (DOT) is.invalid> wrote:
Quote:
Jodawi wrote:
It's common for values like 6.0 to turn into values like
5.9999999999999991 when doing floating point calculations. What's the
best way to display these to users as nice numbers? I've been using
DBL_EPSILON to nudge numbers for formatting purposes for a while now
(see example below).
[...]
ostringstream formattedString;
formattedString << fixed;
// Round .xxx99999999 up by adding a very small number
x *= (1.0 + DBL_EPSILON);

formattedString << setprecision(f.digits);
formattedString << x;

How can you be sure that this will not turn 6.0 into
6.00000000000000178? It does on my implementation,
when I set the precision to 18.

Because I never display that much precision.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Falk Tannhäuser
Guest





PostPosted: Mon Nov 15, 2004 9:39 pm    Post subject: Re: Formatting numbers nicely with DBL_EPSILON Reply with quote

Jodawi wrote:
Quote:
Falk Tannhäuser <clcppm-poster (AT) this (DOT) is.invalid> wrote:
Jodawi wrote:
It's common for values like 6.0 to turn into values like
5.9999999999999991 when doing floating point calculations. What's the
best way to display these to users as nice numbers?
[...]
How can you be sure that this will not turn 6.0 into
6.00000000000000178? It does on my implementation,
when I set the precision to 18.

Because I never display that much precision.

So, what is the problem you are trying to solve, then?
std::cout << std::setprecision(15) << std::showpoint << 5.9999999999999991 << 'n';
already displays 6.00000000000000 (or simply 6 when omitting 'showpoint') on my
platform. Only when setting the precision to 16 or 17, I get 5.999999999999999 or
5.9999999999999991 respectively. That is how it ought to be IMO - rounding in the
right direction is already done by the output function.

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jodawi
Guest





PostPosted: Tue Nov 16, 2004 11:34 am    Post subject: Re: Formatting numbers nicely with DBL_EPSILON Reply with quote

[email]cbarron3 (AT) ix (DOT) netcom.com[/email] (Carl Barron) wrote:
Quote:
Jodawi <jodawi (AT) gmail (DOT) com> wrote:

ostringstream formattedString;
formattedString << fixed;
// Round .xxx99999999 up by adding a very small number
x *= (1.0 + DBL_EPSILON);

Its not going to work in general since the expression
(1.+DBL_EPSILON) == 1. , evaluates to true.

I think the meaning (if not the technical definition) of DBL_EPSILON
is the smallest representable number such that (1.+DBL_EPSILON) > 1. I
just tested and (1.+DBL_EPSILON) == 1. evaluates to false for my
current compiler.

Quote:
so except for errors in
the numeric value of DBL_EPSILON [from binary to decimal conversion,
since on most modern hardware, DBL_EPSILON in the internal format is an
exact number. Write the number to an ostringstream using the formating
manipulators currently used on the output stream and then
examine the resulting string for a trailing sequence of '9's and modify
the string. Then output it.

That's more complicated and slower than multiplying by a number that's
slightly larger than 1.0, so I'd like to avoid it. Since I don't care
about trying to display more than 10 or so digits of precision, I
could mulitply by something like (1.0 + 10.0*DBL_EPSILON), but I don't
know of a case where (1.0 + DBL_EPSILON) fails.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jodawi
Guest





PostPosted: Wed Nov 17, 2004 11:13 am    Post subject: Re: Formatting numbers nicely with DBL_EPSILON Reply with quote

Falk Tannhäuser <clcppm-poster (AT) this (DOT) is.invalid> wrote:
Quote:
Jodawi wrote:
Falk Tannhäuser <clcppm-poster (AT) this (DOT) is.invalid> wrote:
Jodawi wrote:
It's common for values like 6.0 to turn into values like
5.9999999999999991 when doing floating point calculations. What's the
best way to display these to users as nice numbers?

How can you be sure that this will not turn 6.0 into
6.00000000000000178? It does on my implementation,
when I set the precision to 18.

Because I never display that much precision.

So, what is the problem you are trying to solve, then?
std::cout << std::setprecision(15) << std::showpoint << 5.9999999999999991 << 'n';
already displays 6.00000000000000 (or simply 6 when omitting 'showpoint') on my
platform. Only when setting the precision to 16 or 17, I get 5.999999999999999 or
5.9999999999999991 respectively. That is how it ought to be IMO - rounding in the
right direction is already done by the output function.

Your example is giving me 6.00000000000000 without '<< fixed',
and 5.999999999999999 with '<< fixed', with both MS Visual C++ and MW
Codewarrior.

The most recent complaint was that 2.35*1.11 displayed to 3 decimal
places as 2.608 instead of 2.609. (2.35*1.11==2.6085, filtering down
from our GUI as 2.6084999999999998. "2.35*1.11" is a string typed in
by the user and parsed.)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
velthuijsen
Guest





PostPosted: Thu Nov 18, 2004 12:24 am    Post subject: Re: Formatting numbers nicely with DBL_EPSILON Reply with quote

Quote:
That's more complicated and slower than multiplying by a number that's
slightly larger than 1.0, so I'd like to avoid it. Since I don't care
about trying to display more than 10 or so digits of precision, I
could mulitply by something like (1.0 + 10.0*DBL_EPSILON), but I don't
know of a case where (1.0 + DBL_EPSILON) fails.

easy, double epsilon is defined for my compiler as
2.2204460492503131e-016 all I need to do is give you a double that
doesn't get a precision of e-16 but say e-14, 101.00000000000001
would do. Adding DBL_EPSILON to that might or might not increase the
tail end (most likely won't).
Then there is the chance that your compiler doesn't get a smaller
number like the
5.9999999999999991 one you mentioned. There is an equal chance that
you get a 6.0000000000000009 value and adding DBL_EPSILON would not
remove your problem.
Then there is that for me adding DBL_EPSILON to 5.9999999999999991
would generate a number 5.9999999999999991. I'd need atleast
2*DBL_EPSILON (using my compiler) to get that number rounded to 6.

But you've already mentioned the solution to your problem.
You only need 10 places behind the point. Then only display 10 behind
the point.
FormattedString.precision(10);
Now if I feed in 5.9999999999999991 I just get a 6 displayed.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Falk Tannhäuser
Guest





PostPosted: Thu Nov 18, 2004 12:26 pm    Post subject: Re: Formatting numbers nicely with DBL_EPSILON Reply with quote

Jodawi wrote:
Quote:
I think the meaning (if not the technical definition) of DBL_EPSILON
is the smallest representable number such that (1.+DBL_EPSILON) > 1. I
just tested and (1.+DBL_EPSILON) == 1. evaluates to false for my
current compiler.

On my platform, with
double x = 1.0 + DBL_EPSILON * 0.50001;
I have x > 1.0 (and x == 1.0 + DBL_EPSILON).

The exact definition is "the difference between 1 and the least value
greater than 1 that is representable." See § 18.2.1.2/20, DBL_EPSILON
is the same as std::numeric_limits<double>::epsilon() .

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Falk Tannhäuser
Guest





PostPosted: Thu Nov 18, 2004 12:27 pm    Post subject: Re: Formatting numbers nicely with DBL_EPSILON Reply with quote

Jodawi wrote:

Quote:
Your example is giving me 6.00000000000000 without '<< fixed',
and 5.999999999999999 with '<< fixed', with both MS Visual C++ and MW
Codewarrior.

Which still looks correct to me: the original value was 5.9999999999999991
(15 nines after the point), and the result without '<< fixed' is correctly
rounded up since displays only 14 digits after the point, while the result
with '<< fixed' displays 15 digits after the point - which is why rounding
down is appropriate.

Quote:
The most recent complaint was that 2.35*1.11 displayed to 3 decimal
places as 2.608 instead of 2.609. (2.35*1.11==2.6085, filtering down
from our GUI as 2.6084999999999998. "2.35*1.11" is a string typed in
by the user and parsed.)

That's another matter: The exact value would lie exactly in the middle
between 2.608 instead of 2.609 but is not representable in binary floating
point format.
It is quite easy to construct cases where rounding errors sum up enough
to defeat the "multiply by (1.0 + DBL_EPSILON)" method for this one:

double foo(double x)
{
unsigned const N = 5; // May need to adjust this to reproduce the problem
double a[N];
a[0] = x;
for(unsigned i=1; i for(unsigned i=N-1; i>0; --i) a[i-1] = a[i] * 3.0;
return a[0];
}
....

double const d = 1.0 + std::numeric_limits<double>::epsilon();
double x = foo(2.6085);
std::cout << std::fixed << std::setprecision(3) << x << ' ' << x*d << 'n';

rounds down (displays "2.608 2.608") on my machine.

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.