Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the least possible denominator/divisor value?

Tags:

c++

c++11

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?

like image 644
ctanakul Avatar asked Jul 12 '18 00:07

ctanakul


People also ask

How do I find the least common denominator?

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.

What is a least common denominator in math?

The least common denominator is the smallest number of all the common multiples of the denominators when 2 or more fractions are given.

What is the lowest common denominator of 1 2 and 1 3?

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.

What is the least common denominator of 1 4?

The LCM of 1 and 4 is 4.


1 Answers

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.

like image 187
paxdiablo Avatar answered Sep 22 '22 03:09

paxdiablo