Dividing 1/2 in C#

Asked

Viewed 552 times

5

In some videos on numberphile about the Zeno’s Paradox the teacher of the video tried to explain how this paradox worked using hands to hit Palamas (I do not know how to explain this but I will leave the video there in the question, watch the video please is easier to understand).

Briefly in the video is shown that every time you will clap you shorten half the distance as in the example below.

1/2 = 0.5
0.5 / 2 = 0.25

To return these divisions you can use the following code:

static void Main(string[] args)
        {
            int i = 0;
            decimal x = 1m;
            decimal dividir = x / 2m;
            do
            {
                i++;
                Console.WriteLine("{0}:{1}", i, dividir);
                dividir = dividir / 2m;

            } while (dividir != 0);
            Console.ReadKey();
        }

At the end are returned 93 results.

That’s right?

It shouldn’t have much more results than that?

output1 output2

  • Tried debugging 92 and see what the split value is?

  • No, it’s just that as this is not a calculator and it’s far from being also the first time I ran the program and went to compare in Windows calculator the result of the 17° split got like this. 7,62939453125e-6 and the application is different, I kept thinking about it and ended up not even debugging to know the result.

  • Each variable type has a maximum bit capacity. Picking a class with a larger capacity should increase the amount of results received.

  • Yeah, totally forgot about it, good not totally but anyway, just reading the comments and reply I remembered it. Thank you

  • @Rodolfoolivieri I didn’t suggest you use the double because I thought you wanted absolute accuracy. Yes, the double provides much larger numbers but with deviations. If this is ok for you, and is in many applications, great. But know that the result will not be exactly the same.

  • This little program there was more for a test anyway, I’m not using anywhere, just for learning. I mean I hosted him on github but nothing serious

Show 1 more comment

2 answers

9


All right, the guy Decimal has a limited accuracy, if you want greater precision you should use another structure or create a specific one. Take a look at BigInteger.

Obviously the BigInteger does not solve your problem, just used as reference to see a structure more accurately.

using static System.Console;
                    
public class Program {
    public static void Main() {
        var dividir = 1m;
        for (var i = 1; dividir != 0; i++) WriteLine($"{i}:{(dividir /= 2m)}");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

If you want more precision but with deviations you can use the double. Placed in the .NET Fiddle some interactions to see how it looks.

  • hmm I thought I needed to use the math class, I even got to take a look at it

  • Math It’s not really a class, it’s a collection of methods that do some operations but it’s not a data structure, it wouldn’t solve anything. I don’t know exactly where you’re going with this, but I tried to at least resolve your main question. I don’t know if there is any intention of representing something infinite. This is a very abstract concept for a computer to deal with.

  • It was exactly this answer that I needed, I came to think that Decimal wasn’t big enough but for some reason I didn’t change it for another test, thank you anyway.

3

Basically what happens is that the last division you get before it is == 0 has a number of decimal places that fit in one decimal.

Thus the value that should be for it to become != 0 does not fit in the DECIMAL, so the divided value is == 0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.