I am writing a complete dual float function for Arduino (it does not matter, but I could not find any "correct" ones), and I am doing this check:
if (d < 0) { d *= -1; bin += "-"; }
I know because of floating point inaccuracies, double equality is fictitious. Is it safe to do this? Or should I stick with this (which I use in subsequent parts of my code anyway)
int compareNums(double x, double y) { if (abs(x - y) <= EPSILON) { return 0; } else if (x > y) { return 1; } else { return -1; } }
And a few simple questions: does it matter if I do d < 0 or d < 0.0 ?
I multiply double d by 10 until it has a fractional part, so I do a check similar to d == (int) d . I am wondering what good epsilon to use (I used it here http://msdn.microsoft.com/en-us/library/6x7575x3(v=vs.80).aspx ), since I do not want an infinite loop. According to article 0.000000119209 , this is the smallest distinguishable difference for floats or something like that.
thanks
source share