Solved

For loop unexpected end value

Posted on 2008-10-29
3
202 Views
Last Modified: 2013-12-17
Hi, When experimenting with a for loop in c# I have an unexpected final value. The first code loop below shows an expected result with the values 0 - 1 printed to the output window in steps of 0.1. However the second example ends with a value of 0.95! Other step values such as 0.2, 0.25 work ok and ends on 1. But for some reason any step values below 0.1 don't end on 1, such as 0.05, 0.025, 0.01 etc.

Any explanation for this would be greatly appreciated. plus how I can make sure it always ends on 1 assuming the step value is entirely divisible into 1 with no remainder.
for (double i = 0; i <= 1; i += 0.1)

{

   Debug.WriteLine(i);

}
 

for (double j = 0; j <= 1; j += 0.05)

{

   Debug.WriteLine(j);

}

Open in new window

0
Comment
Question by:dg_dibs
  • 3
3 Comments
 
LVL 18

Accepted Solution

by:
Priest04 earned 125 total points
ID: 22836041
You should never count on having correct resultt when dealing with double variables. Why your code is not displaying the last j value (which should be 1) is because it is slightly larger than 1

ex

1.0000000000252

This is why, when using doubles in loop, you need to do next




for (double i = 0; i <= 1.01; i += 0.1)

{

   Debug.WriteLine(i);

}

 

for (double j = 0; j <= 1.01; j += 0.05)

{

   Debug.WriteLine(j);

}

Open in new window

0
 
LVL 18

Expert Comment

by:Priest04
ID: 22836056
Add watch to j varaible and see what its value is when it loops.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22841635
May I ask why B? What was incomplete in my answer?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now