Dividing 1/2 in C #

5

In some videos in numberphile on the Zeno's Paradox the teacher of the video tried to explain how this paradox worked using the hands to beat palamas (I'm not sure how to explain this but I'll leave the video there in question, please watch the video is easier to understand) .

Briefly in the video is shown that every time you 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, 93 results are returned.

Is that right?

Should not it have more results than that?

    
asked by anonymous 15.01.2015 / 14:38

2 answers

9

That's right, type Decimal has a limited precision, if you want greater accuracy you must use another structure or create a specific one. Take a look at BigInteger .

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

Running .

If you want more precision but with deviations you can use double . I put in dotNetFiddle for some interactions to see how it goes.

    
15.01.2015 / 14:45
3

Basically what happens is that the last division you get before it is == 0 has a number of DECIMAL houses that fit into a DECIMAL.

So the value it should be for it to become! = 0 does not fit in DECIMAL, so the divided value stays == 0

    
15.01.2015 / 14:49