简体   繁体   中英

Double precision?

I have a situation where I'm sending a number to a method, it divides that number into 6 and returns a List with the number 6 divided by n number of items. I'm pulling the dividing number from a dictionary.count and combining the returning list with the dictionary I pulled it from. Returned list, For some reason it does not always return the correct number of items. It works fine through 12. But then it's predictable, if not reliable. The following numbers return a list with 1 less index than is needed...13,15,18,23,25,20,27,28,30.....The code below was pulled from a larger project.

public void DivTest()
{
    double value;
    Double.TryParse(textBox1.Text, out value);

    double div = 6 / value;
    int count = 1;

    StringBuilder sb = new StringBuilder();

    for (double d = div; d <= 6; d += div)
    {
        sb.Append(count.ToString()).Append("  :  ")
            .Append(d.ToString("0.0000")).Append("  :  ")                                                                                                                             
            .Append(div.ToString("0.0000")).AppendLine();

        count++;
    }

    label1.Text = sb.ToString();
}

If you add this code to a form with a default named textbox, label and button, it probably won't work correctly with you either. The last line should always be 6, but it's with the numbers I've mentioned. I thought it was a rounding issue, but I'm not using rounding in this example. Any ideas? Thanks.

It is a rounding problem, or rather a problem with the limited precision of numbers.

The rounding occurs when you do the division. The result can't be represented exactly, it's limited by the precision.

When the result is rounded up, you will end up with a slightly larger number when you add them up, for example 6.000000000000001 instead of 6. As that is larger than 6, it won't enter the last iteration in the loop.

You would fix this by using an integer variable for the loop, simply looping from 1 to value .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM