Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

For loop unexpected end value

Posted on 2008-10-29
3
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 18

Accepted Solution

by:
Priest04 earned 375 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 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