.Net and Math.Floor()
Wednesday, June 21st, 2006Most of my time at work is spent working with .Net and C#. I just discovered what I consider to be a significant flaw in .Net (1.1).
The Floor function is a standard function across many languages. Basically you give it a decimal and it removes everything after the decimal point, giving you an integer.
The problem is that the Microsoft implementation of the Floor returns a double which is a floating point number. Floating point numbers are an approximation. For example if you set a floating point variable to 5, it might actually hold the value 5.00000002004. For most maths operations this is sufficient.
In the case of Floor, this is retarded. This means that suppose I give Floor a value of 5.8, instead of getting back 5 (or 5.0), I get back 5.000000203. Earth to Microsoft: If I have called Floor I want an int (of sufficient precision).
In Microsoft’s defence there are two reasons why this makes some sense:
1. There are precision issues, a int64 doesn’t cover all the available returns
2. All the other Math functions work with doubles only
Reason 2 isn’t a good reason. The reality that the Floor is not truly Math Functions in the same way that say Sin and Cos are. When you use Floor you deliberatly losing precision. The only reason to do this is for user display, or when you are not truly doing a mathematical operation.
Regardless, this is still a stupid decision by Microsoft. At least they should provide overloads that take a decimal (which is not floating point) and return an int of sufficient precision.