Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
math.hh
Go to the documentation of this file.
1 #ifndef VCSN_MISC_MATH_HH
2 # define VCSN_MISC_MATH_HH
3 
4 # include <vcsn/misc/attributes.hh>
5 # include <vcsn/misc/raise.hh>
6 
7 namespace vcsn
8 {
9  namespace detail
10  {
11  // Greatest common divisor.
12  ATTRIBUTE_PURE
13  inline
14  unsigned int gcd(unsigned int a, unsigned int b)
15  {
16  require(b, "gcd: rhs cannot be zero");
17  while (b)
18  {
19  unsigned int t = a;
20  a = b;
21  b = t % b;
22  }
23  return a;
24  }
25 
26  // Lowest common multiple
27  ATTRIBUTE_PURE
28  inline
29  unsigned int lcm(unsigned int a, unsigned int b)
30  {
31  return a / gcd(a, b) * b;
32  }
33  }
34 }
35 
36 #endif // VCSN_MISC_MATH_HH
ATTRIBUTE_PURE unsigned int lcm(unsigned int a, unsigned int b)
Definition: math.hh:29
ATTRIBUTE_PURE unsigned int gcd(unsigned int a, unsigned int b)
Definition: math.hh:14
variadic_mul_mixin< detail::b_impl > b
Definition: fwd.hh:38
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39