Hello! I have the following task: to write in C ++ a method that calculates the GCD for 2 int numbers OR double (template class).
I took the Euclidean algorithm with a few changes:
template <typename T> T rational<T>::nod(T a, T b) { if (a == b) { return a; } if (a > b) { T tmp = a; a = b; b = tmp; } return nod(a, b - a); } Works with:
rational<double> rat(5, 10);//рациональная дробь 5/10 rat.nod(5, 10);//вычисление НОД 5 и 10 And does not work when:
rational<double> rat(5.1, 10);//рациональная дробь 5/10 rat.nod(5.1, 10);//вычисление НОД 5.1 и 10 Question: why is this happening, and how to fix it in order to work in both variants? Thanks in advance for the answers!
doubledata type. In particular, checking for equality ofdoublemeaningless. - VladDrational<int>. - VladD