未验证 提交 167618f7 编写于 作者: A alexey-milovidov 提交者: GitHub

Merge pull request #1538 from samael500/stable

Add missing test for GCD/LCM functions.
...@@ -439,11 +439,11 @@ struct GCDImpl ...@@ -439,11 +439,11 @@ struct GCDImpl
template <typename Result = ResultType> template <typename Result = ResultType>
static inline Result apply(A a, B b) static inline Result apply(A a, B b)
{ {
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<A>::Type(b)); throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b));
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(b), typename NumberTraits::ToInteger<A>::Type(a)); throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<B>::Type(b), typename NumberTraits::ToInteger<A>::Type(a));
return boost::math::gcd( return boost::math::gcd(
typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<Result>::Type(a),
typename NumberTraits::ToInteger<A>::Type(b)); typename NumberTraits::ToInteger<Result>::Type(b));
} }
}; };
...@@ -455,11 +455,11 @@ struct LCMImpl ...@@ -455,11 +455,11 @@ struct LCMImpl
template <typename Result = ResultType> template <typename Result = ResultType>
static inline Result apply(A a, B b) static inline Result apply(A a, B b)
{ {
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<A>::Type(b)); throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b));
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(b), typename NumberTraits::ToInteger<A>::Type(a)); throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<B>::Type(b), typename NumberTraits::ToInteger<A>::Type(a));
return boost::math::lcm( return boost::math::lcm(
typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<Result>::Type(a),
typename NumberTraits::ToInteger<A>::Type(b)); typename NumberTraits::ToInteger<Result>::Type(b));
} }
}; };
......
256
11
64
1
2
1
1
1
2147483647
1
5120
121
256
1
4
735
64770
4611686011984936962
4611686011984936962
2147483648
256
11
64
1
2
1
1
5120
121
256
1
4
735
64770
-- test gcd
select gcd(1280, 1024);
select gcd(11, 121);
select gcd(-256, 64);
select gcd(1, 1);
select gcd(4, 2);
select gcd(15, 49);
select gcd(255, 254);
select gcd(2147483647, 2147483646);
select gcd(4611686011984936962, 2147483647);
select gcd(-2147483648, 1);
-- test lcm
select lcm(1280, 1024);
select lcm(11, 121);
select lcm(-256, 64);
select lcm(1, 1);
select lcm(4, 2);
select lcm(15, 49);
select lcm(255, 254);
select lcm(2147483647, 2147483646);
select lcm(4611686011984936962, 2147483647);
select lcm(-2147483648, 1);
-- test gcd float will cast to int
select gcd(1280.1, 1024.1);
select gcd(11.1, 121.1);
select gcd(-256.1, 64.1);
select gcd(1.1, 1.1);
select gcd(4.1, 2.1);
select gcd(15.1, 49.1);
select gcd(255.1, 254.1);
-- test lcm float cast to int
select lcm(1280.1, 1024.1);
select lcm(11.1, 121.1);
select lcm(-256.1, 64.1);
select lcm(1.1, 1.1);
select lcm(4.1, 2.1);
select lcm(15.1, 49.1);
select lcm(255.1, 254.1);
...@@ -64,3 +64,13 @@ abs(a) ...@@ -64,3 +64,13 @@ abs(a)
------ ------
Calculates the absolute value of the number 'a'. That is, if a< 0, it returns -a. Calculates the absolute value of the number 'a'. That is, if a< 0, it returns -a.
For unsigned types, it doesn't do anything. For signed integer types, it returns an unsigned number. For unsigned types, it doesn't do anything. For signed integer types, it returns an unsigned number.
gcd(a, b)
---------
Calculates the greatest common divisor of the numbers.
When dividing by zero or when dividing a minimal negative number by minus one, an exception is thrown.
lcm(a, b)
---------
Calculates the least common multiple of the numbers.
When dividing by zero or when dividing a minimal negative number by minus one, an exception is thrown.
...@@ -73,3 +73,13 @@ abs(a) ...@@ -73,3 +73,13 @@ abs(a)
Вычисляет абсолютное значение для числа a. То есть, если a &lt; 0, то возвращает -a. Вычисляет абсолютное значение для числа a. То есть, если a &lt; 0, то возвращает -a.
Для беззнаковых типов ничего не делает. Для чисел типа целых со знаком, возвращает число беззнакового типа. Для беззнаковых типов ничего не делает. Для чисел типа целых со знаком, возвращает число беззнакового типа.
gcd(a, b)
---------
Вычисляет наибольший общий делитель чисел.
При делении на ноль или при делении минимального отрицательного числа на минус единицу, кидается исключение.
lcm(a, b)
---------
Вычисляет наименьшее общее кратное чисел.
При делении на ноль или при делении минимального отрицательного числа на минус единицу, кидается исключение.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册