There are a number of issues here:
float
, orSystem.Single
to give it its full name is a single precision floating point number. This means its precision is very limited. Many operations will lead to slight rounding errors (such as your -1.525879E-06: which is just over one part in a million).The first thing to do is switch to
double
(orSystem.Double
). This is the default floating point type unless you specify otherwise.The second thing to do is understand (and accept) the limited precision inherent in floating point representation. (Eg. one never compares two floating point numbers directly for equality, rather check that the absolute difference is less than some small value, "epsilon":
Math.Abs(a - b) < epsilon
where epsilon is dependent on the scale ofa
andb
.)Finally read What Every Computer Scientist Should Know About Floating-Point Arithmetic.
9/5
(and similar) will be performed as integers (ie. rounded down) because none of the values involved are not integers. Try (9.0 / 5.0
). (Strictly because this is part of a larger expression that type of the first term will set the type (yourcel
), however it adds cality code code to make it clear that these operations are floating point and not integral.)