Know what you’re doing
There is no silver bullet. You have to choose wisely.
- If you are comparing against zero, then relative epsilons and ULPs based comparisons are usually meaningless. You’ll need to use an absolute epsilon, whose value might be some small multiple of FLT_EPSILON and the inputs to your calculation. Maybe.
- If you are comparing against a non-zero number then relative epsilons or ULPs based comparisons are probably what you want. You’ll probably want some small multiple of FLT_EPSILON for your relative epsilon, or some small number of ULPs. An absolute epsilon could be used if you knew exactly what number you were comparing against.
- If you are comparing two arbitrary numbers that could be zero or non-zero then you need the kitchen sink. Good luck and God speed.
Know what you’re doing
There is no silver bullet. You have to choose wisely.
- If you are comparing against zero, then relative epsilons and ULPs based comparisons are usually meaningless. You’ll need to use an absolute epsilon, whose value might be some small multiple of FLT_EPSILON and the inputs to your calculation. Maybe.
- If you are comparing against a non-zero number then relative epsilons or ULPs based comparisons are probably what you want. You’ll probably want some small multiple of FLT_EPSILON for your relative epsilon, or some small number of ULPs. An absolute epsilon could be used if you knew exactly what number you were comparing against.
- If you are comparing two arbitrary numbers that could be zero or non-zero then you need the kitchen sink. Good luck and God speed.
Know what you’re doing
There is no silver bullet. You have to choose wisely.
- If you are comparing against zero, then relative epsilons and ULPs based comparisons are usually meaningless. You’ll need to use an absolute epsilon, whose value might be some small multiple of FLT_EPSILON and the inputs to your calculation. Maybe.
- If you are comparing against a non-zero number then relative epsilons or ULPs based comparisons are probably what you want. You’ll probably want some small multiple of FLT_EPSILON for your relative epsilon, or some small number of ULPs. An absolute epsilon could be used if you knew exactly what number you were comparing against.
- If you are comparing two arbitrary numbers that could be zero or non-zero then you need the kitchen sink. Good luck and God speed.
- If you are downgrading
doubles tofloats, then you might lose precision, and incorrectly report two differentdoubles as equal (as paxdiablo paxdiablo points out.) - If you are upgrading identical
floats todouble, then the added precision won't be a problem unless you are comparing afloatwith adouble(Say you'd got 1.234 in float, and you only had 4 decimal digits of accuracy, then the double 1.2345 MIGHT represent the same value as the float. In this case you'd probably be better to do the comparison at the precision of thefloat, or more generally, at the error level of the most inaccurate representation in the comparison). - If you know the number you'll be comparing with, you can follow the advice quoted above.
- If you're comparing arbitrary numbers (which could be zero or non-zero), there's no way to compare them correctly in all cases - pick one comparison and know its limitations.
- If you are downgrading
doubles tofloats, then you might lose precision, and incorrectly report two differentdoubles as equal (as paxdiablo points out.) - If you are upgrading identical
floats todouble, then the added precision won't be a problem unless you are comparing afloatwith adouble(Say you'd got 1.234 in float, and you only had 4 decimal digits of accuracy, then the double 1.2345 MIGHT represent the same value as the float. In this case you'd probably be better to do the comparison at the precision of thefloat, or more generally, at the error level of the most inaccurate representation in the comparison). - If you know the number you'll be comparing with, you can follow the advice quoted above.
- If you're comparing arbitrary numbers (which could be zero or non-zero), there's no way to compare them correctly in all cases - pick one comparison and know its limitations.
- If you are downgrading
doubles tofloats, then you might lose precision, and incorrectly report two differentdoubles as equal (as paxdiablo points out.) - If you are upgrading identical
floats todouble, then the added precision won't be a problem unless you are comparing afloatwith adouble(Say you'd got 1.234 in float, and you only had 4 decimal digits of accuracy, then the double 1.2345 MIGHT represent the same value as the float. In this case you'd probably be better to do the comparison at the precision of thefloat, or more generally, at the error level of the most inaccurate representation in the comparison). - If you know the number you'll be comparing with, you can follow the advice quoted above.
- If you're comparing arbitrary numbers (which could be zero or non-zero), there's no way to compare them correctly in all cases - pick one comparison and know its limitations.
As long as you are not mixing promoted floats and natively calculated doubles in your comparison you should be ok, but take care:
Comparing floats (or doubles) for equality is difficultComparing floats (or doubles) for equality is difficult - see this lengthy but excellent discussion. Here
Here are some highlights:
As long as you are not mixing promoted floats and natively calculated doubles in your comparison you should be ok, but:
Comparing floats (or doubles) for equality is difficult - see this lengthy but excellent discussion. Here are some highlights:
As long as you are not mixing promoted floats and natively calculated doubles in your comparison you should be ok, but take care:
Comparing floats (or doubles) for equality is difficult - see this lengthy but excellent discussion.
Here are some highlights: