-1

Newbie in C#, trying to work out a simple calculation.

float old x=300   
float Distance=300   
float pitch=0.8
int sign=1  

new x= old x - (sign * (Distance % pitch) * 0.5 f)

The value generated by program for new x is 299.6 (which I don't understand).

The value for (Distance % pitch) is 0.7999955. If you calculate manually 300 modulo 0.8 is 0. I am guessing modulo function behaves differently for float values but i don't know how. Or it is calculated as 300 percentage of 0.8?

Explanation on this will be much appreciated.

Amir
  • 10,600
  • 9
  • 48
  • 75
Rockstar
  • 35
  • 2

1 Answers1

1

Never expect binary floating-point calculations (using float or double) on decimal floating point values (e.g. 0.8) to give exact results.

The problem is that most of decimal numbers cannot be represented exactly in a binary floating-point format. You can think of it as trying to represent 1/3 in a decimal notation. It's not possible.

In your case, 0.8f is not really 0.8. If you cast the value to double to get some extra precision, you will see it's closer to 0.800000011920929

float pitch = 0.8f;
Console.WriteLine((double)pitch);  // prints 0.800000011920929
Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39