libstdc++
numeric
Go to the documentation of this file.
00001 // <experimental/numeric> -*- C++ -*-
00002 
00003 // Copyright (C) 2015-2016 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file experimental/numeric
00026  *  This is a TS C++ Library header.
00027  */
00028 
00029 //
00030 // N4336 Working Draft, C++ Extensions for Library Fundamentals, Version 2
00031 //
00032 
00033 #ifndef _GLIBCXX_EXPERIMENTAL_NUMERIC
00034 #define _GLIBCXX_EXPERIMENTAL_NUMERIC 1
00035 
00036 #pragma GCC system_header
00037 
00038 #if __cplusplus <= 201103L
00039 # include <bits/c++14_warning.h>
00040 #else
00041 
00042 #include <experimental/type_traits>
00043 #include <cmath>
00044 
00045 namespace std _GLIBCXX_VISIBILITY(default)
00046 {
00047 namespace experimental
00048 {
00049 inline namespace fundamentals_v2
00050 {
00051 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00052 
00053 #define __cpp_lib_experimental_gcd_lcm 201411
00054 
00055   // Greatest common divisor
00056   template<typename _Mn, typename _Nn>
00057     constexpr common_type_t<_Mn, _Nn>
00058     gcd(_Mn __m, _Nn __n)
00059     {
00060       static_assert(is_integral<_Mn>::value, "arguments to gcd are integers");
00061       static_assert(is_integral<_Nn>::value, "arguments to gcd are integers");
00062 
00063       return __m == 0 ? std::abs(__n)
00064         : __n == 0 ? std::abs(__m)
00065         : fundamentals_v2::gcd(__n, __m % __n);
00066     }
00067 
00068   // Least common multiple
00069   template<typename _Mn, typename _Nn>
00070     constexpr common_type_t<_Mn, _Nn>
00071     lcm(_Mn __m, _Nn __n)
00072     {
00073       static_assert(is_integral<_Mn>::value, "arguments to lcm are integers");
00074       static_assert(is_integral<_Nn>::value, "arguments to lcm are integers");
00075 
00076       return (__m != 0 && __n != 0)
00077         ? (std::abs(__m) / fundamentals_v2::gcd(__m, __n)) * std::abs(__n)
00078         : 0;
00079     }
00080 
00081 _GLIBCXX_END_NAMESPACE_VERSION
00082 } // namespace fundamentals_v2
00083 } // namespace experimental
00084 } // namespace std
00085 
00086 #endif // __cplusplus <= 201103L
00087 
00088 #endif // _GLIBCXX_EXPERIMENTAL_NUMERIC