473,381 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

double type problem

Hi,

I have a strange problem about a simple sum operation on a double variable.
There is a loop in my application like this :

double _max = 0.4;
double _step = 0.1;

for (double d = 0.0; i < _max || double_equal (i, _max); i += step)
{
// Do something...
}

double_equal() is an epsilon based equality test function.

The problem is i's value on 4. turn. When I watch I see 0.30000000000000004
insted of 0.3
This is problematic because I compare it some other doubles in the loop but
even epsilon based approximitation will fail in this case.

It is interesting that this only appears on loop's 4. turn. On 5.turn the
value is 0.4, excellent!

What is the reason and how can I overcome this?

Thanks in advance.
Aug 14 '08 #1
7 1563
Kürşat <ku***********@gmail.comwrote:
I have a strange problem about a simple sum operation on a double variable.
There is a loop in my application like this :

double _max = 0.4;
double _step = 0.1;

for (double d = 0.0; i < _max || double_equal (i, _max); i += step)
{
// Do something...
}

double_equal() is an epsilon based equality test function.

The problem is i's value on 4. turn. When I watch I see 0.30000000000000004
insted of 0.3
This is problematic because I compare it some other doubles in the loop but
even epsilon based approximitation will fail in this case.
Sounds like your epsilon is too small then.
It is interesting that this only appears on loop's 4. turn. On 5.turn the
value is 0.4, excellent!

What is the reason and how can I overcome this?
See http://pobox.com/~skeet/csharp/floatingpoint.html and
http://pobox.com/~skeet/csharp/decimal.html

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 14 '08 #2
I used the Epsilon provided by the double class and expected deviations
related to the double arithmetics will fall the range that this provided
epsilon determines.

It seems I should determine an appropriate epsilon value instead of relying
on the provided epsilon.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Kürşat <ku***********@gmail.comwrote:
I have a strange problem about a simple sum operation on a double
variable.
There is a loop in my application like this :

double _max = 0.4;
double _step = 0.1;

for (double d = 0.0; i < _max || double_equal (i, _max); i += step)
{
// Do something...
}

double_equal() is an epsilon based equality test function.

The problem is i's value on 4. turn. When I watch I see
0.30000000000000004
insted of 0.3
This is problematic because I compare it some other doubles in the loop
but
even epsilon based approximitation will fail in this case.
Sounds like your epsilon is too small then.
It is interesting that this only appears on loop's 4. turn. On 5.turn the
value is 0.4, excellent!

What is the reason and how can I overcome this?
See http://pobox.com/~skeet/csharp/floatingpoint.html and
http://pobox.com/~skeet/csharp/decimal.html

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 14 '08 #3
Kürsat <ku***********@gmail.comwrote:
I used the Epsilon provided by the double class and expected deviations
related to the double arithmetics will fall the range that this provided
epsilon determines.

It seems I should determine an appropriate epsilon value instead of relying
on the provided epsilon.
Yes. If you look at the documentation for double.Epsilon you'll see why
it's really not suitable for this kind of use.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 14 '08 #4
On Aug 14, 2:44*pm, "Kürsat" <kursatthek...@gmail.comwrote:
I used the Epsilon provided by the double class and expected deviations
related to the double arithmetics will fall the range that this provided
epsilon determines.
MSDN has this to say on Double.Epsilon:

"If you create a custom algorithm that determines whether two floating-
point numbers can be considered equal, you must use a value that is
greater than the Epsilon constant to establish the acceptable absolute
margin of difference for the two values to be considered equal.
(Typically, that margin of difference is many times greater than
Epsilon.)"

I don't see why you don't just use <= in your specific case here,
since the number will fall in the range you require... but if you
really need it, you'd probably want to just use decimal (it has
similar issues as well, but they're more predictable because it's
decimal and not binary - so you can be sure that any decimal number is
represented exactly as you wrote it).
Aug 14 '08 #5
To see why the "<=" operator would not be appropriate let's look at an
example:

I want to add numbers between 0.0-0.3 stepping 0.1 (0.0, 0.1, 0.2, 0.3) to a
listbox:

double _start = 0.0;
double _end = 0.3;
double _step = 0.1;

for (double d = _start; d <= _end; d+=_step)
{
lb.Items.Add (d.ToSting());
}

This adds "0.0", "0.1" and "0.2" to the ListBox but not "0.3" bacause on 4.
turn, d contains the value 0.30000000000000004 which is neither less than
nor equal to 0.3.
"Pavel Minaev" <in****@gmail.comwrote in message
news:ce**********************************@y21g2000 hsf.googlegroups.com...
On Aug 14, 2:44 pm, "Kürsat" <kursatthek...@gmail.comwrote:
I used the Epsilon provided by the double class and expected deviations
related to the double arithmetics will fall the range that this provided
epsilon determines.
MSDN has this to say on Double.Epsilon:

"If you create a custom algorithm that determines whether two floating-
point numbers can be considered equal, you must use a value that is
greater than the Epsilon constant to establish the acceptable absolute
margin of difference for the two values to be considered equal.
(Typically, that margin of difference is many times greater than
Epsilon.)"

I don't see why you don't just use <= in your specific case here,
since the number will fall in the range you require... but if you
really need it, you'd probably want to just use decimal (it has
similar issues as well, but they're more predictable because it's
decimal and not binary - so you can be sure that any decimal number is
represented exactly as you wrote it).
Aug 14 '08 #6
Kürsat wrote:
I used the Epsilon provided by the double class and expected deviations
related to the double arithmetics will fall the range that this provided
epsilon determines.

It seems I should determine an appropriate epsilon value instead of relying
on the provided epsilon.
The provided epsilon value would work if the difference would come from
a single calculation. The difference grows for each iteration, so you
need a larger epsilon value the more iterations you have.

Use an integer for the loop, and calculate the double value from that in
each iteration. That way you don't have any problems with the loop, and
you don't have any accumulative error in the value:

for (int i = 0; i < 4; i++) {
double d = (double)i * 0.1;
// do something...
}

--
Göran Andersson
_____
http://www.guffa.com
Aug 14 '08 #7
Göran Andersson wrote:
Kürsat wrote:
>I used the Epsilon provided by the double class and expected
deviations related to the double arithmetics will fall the range that
this provided epsilon determines.

It seems I should determine an appropriate epsilon value instead of
relying on the provided epsilon.

The provided epsilon value would work if the difference would come from
a single calculation.
No.

Double.Epsilon is not related to the uncertainty of a number x.

It is the smallest number 0.

It is not (even though the name somehow gives the association)
the well known eps (smallest number where x + eps = x).

System.Double.Epsilon is 4.94065645841247e-324

machar eps is 2.22044605E-016

(and even if it were eps, then the uncertainty would not
be eps even on a single calculation)

Arne
Aug 15 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sascha Herpers | last post by:
Hi, I wrote a c dll with a type library to use it in vb. No problem, everything works fine. Now I needed to pass an array of type double to the dll. I defined the function in the type...
3
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
29
by: Gregc. | last post by:
G'day I was wondering if someone can explain the concept of 'returning a Char from a Double' . For example, I have the following code: char getGrade(double mark) { if (mark>= 85) return...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.