I'm writing a code to prevent the zero denominator/divisor to avoid NaN value as a result of the division.
I wonder what could be the least possible denominator value in double in C++ and how to find or what's the reason for that?
Explanation: To find the least common denominator, list out the multiples of both denominators until you find the smallest multiple that is shared by both. Because 28 is the first shared multiple of 4 and 7, it must be the least common denominator for these two fractions.
The least common denominator is the smallest number of all the common multiples of the denominators when 2 or more fractions are given.
The LCD of 1/2 and 1/3 is 6, because LCM(2, 3) = 6; Knowing the LCD, you can find equivalent fractions to yours 1/2 and 1/3 , with the denominator equal to found LCD: 1/2 = 3/6. 1/3 = 2/6.
The LCM of 1 and 4 is 4.
Well, the smallest positive(a) normalised double
in C++ can be obtained with std::numeric_limits<double>::min()
(from the <limits>
header).
However, while you may be able to use that to prevent NaN
values(b), it probably won't help with overflows. For example:
std::numeric_limits<double>::max() / 1.0 => ok
std::numeric_limits<double>::max() / 0.5 => overflow
Preventing that will depend on both the denominator and the numerator.
As for why that is the case, it's because C++ uses IEEE-754 double-precision format for its double
types - it's a limitation of that format.
(a) I've chosen positive values here, the smallest value could be interpreted as the most negative, in which case it would be read as -std::numeric_limits<double>::max()
. But, given your intent is to avoid NaN
, I suspect my assumption is correct.
(b) I'm not entirely sure how you intend to do this, which is why I also discuss overflows - you may want to make it clearer in your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With