Working with floating point values

D

David Veeneman

I'm working on a project that uses floating-point values (doubles), and I'm
being driven crazy by something pretty basic. I understand that it's in the
nature of floating-point calculations to produce values like
0.10000000000000003, when what I really want is 0.1. But is there any way to
eliminate that digit at the end? I've tried rounding, but that simply moves
the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing with
floating-point issues, beyond using Epsilon for zero comparisons? Thanks in
advance.

David Veeneman
Foresight Systems
 
Q

questions

David,

I've had a quick go at your problem and the Math.Round method does seem
to work.

static void Main(string[] args)
{

double TestNumber = 0.100000000003;

Console.WriteLine ( TestNumber.ToString() );

Console.WriteLine ( Math.Round ( TestNumber, 1 ) );
Console.WriteLine ( Math.Round ( TestNumber, 5 ) );


Console.ReadLine();
}

OUTPUT:

0.100000000003
0.1
0.1

Hope this helps
 
R

ranjeet

you could format the number like
String.Format("{0:#,##0.00;#,##0.00;0.00}", myDecimal.ToString()) ;
(assuming you want a string at the end)

Ranjeet.
 
D

David Veeneman

That's what I've always done in the past, but decimals are *much* slower to
process than floating-points. In this project, I'm running some fairly
complex calculations thousands of times each, so the difference between
decimals and doubles is quite noticeable. So, I'm pretty much stuck with
doubles.
 
D

David Veeneman

Thanks-- I'd thought about that, but I need to maintain the values as
doubles. And the time it would take to convert to string, then back to
double, would slow my calculations down noticeably.
 
G

Guest

Rounding won't help because the some floating point numbers simply can't be
exactly represented in binary. ...
http://docs.sun.com/source/806-3568/ncg_goldberg.html#680

If you can't/won't use decimal, you can test conditionals by seeing if a
number is within a range:

double dd = 0.10000000000000003;

if (dd > 0.09 && dd < 0.11) // true, although the number could be 0.091
0.109 ...
// but you can increase the precision in the conditional

If you doing arithmitic with floating point numbers usually you just do the
calculations and round before displaying.
 
G

Guest

If precision is an issue in your calculation, then you have to use decimal.
otherwise, keep using doubles is fine, and use the right output format to
trim off the rounding error when displaying the results.
 
R

Red Forks

David said:
I'm working on a project that uses floating-point values (doubles), and
I'm being driven crazy by something pretty basic. I understand that it's
in the nature of floating-point calculations to produce values like
0.10000000000000003, when what I really want is 0.1. But is there any way
to eliminate that digit at the end? I've tried rounding, but that simply
moves the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing
with floating-point issues, beyond using Epsilon for zero comparisons?
Thanks in advance.

David Veeneman
Foresight Systems
maybe you can use long instead of double, if long's range & precision can
met your requirement.
long l = 0.1000003d * 10; // convert double to long
// calc using long
double d = (double)l / 10; // convert long to double
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top