I would first like to state that I'm new to coding, so there's probably a better way to do this.So I'm working on a quick program to count the number of digits after the decimal in a number. The lines of code that is causing issues are:
when run with x=0.11 this is what it outputs:
Sense I'm learning explaining why this is caused would be helpful, thanks!
this code repeats until it's left with 0while (dx < 1&dx != 0)
{
//count is here to keep track of how many times it's ran
count++;
x = x * 10;
//the writelines are only so I can see what each variable's value is.
Console.WriteLine(dx is {0}",x);
Console.WriteLine("floor x is = {0}", Math.Floor(x));
Console.WriteLine("x - x rounded down = {0}", (x - (Math.Floor(x))));
dx = dx - Math.Floor(x);
Console.WriteLine(x);
}
when run with x=0.11 this is what it outputs:
and this continues until it runs out of numbers to work with and ends as expected.x is 1.1
floor x is = 1
x - x rounded down = 0.1
0.1
x is 1
floor x is = 1
x - x rounded down = 8.88178419700125E-16
8.88178419700125E-16
I have no idea what causes this and would really appreciate any help.0.5
dx is 5
floor dx is = 5
dx - dx rounded down = 0
0
Sense I'm learning explaining why this is caused would be helpful, thanks!