Browse Source

math: add lol::gcd() function.

legacy
Sam Hocevar 6 years ago
parent
commit
8141a4e6f7
7 changed files with 52 additions and 6 deletions
  1. +1
    -1
      src/lol/math/bigint.h
  2. +3
    -2
      src/lol/math/functions.h
  3. +1
    -1
      src/lol/math/half.h
  4. +1
    -1
      src/lol/math/real.h
  5. +1
    -1
      src/t/Makefile.am
  6. +44
    -0
      src/t/math/numbers.cpp
  7. +1
    -0
      src/t/test-math.vcxproj

+ 1
- 1
src/lol/math/bigint.h View File

@@ -25,7 +25,7 @@
namespace lol
{

/* This is OUR namespace. Don't let Windows headers fuck with it. */
/* This is OUR namespace. Don't let Windows headers mess with it. */
#undef min
#undef max



+ 3
- 2
src/lol/math/functions.h View File

@@ -29,7 +29,7 @@
namespace lol
{

/* This is OUR namespace. Don't let Windows headers fuck with it. */
/* This is OUR namespace. Don't let Windows headers mess with it. */
#undef min
#undef max

@@ -256,7 +256,8 @@ LOL_ATTR_NODISCARD static inline ldouble round(ldouble x) { return std::round(x)
LOL_ATTR_NODISCARD static inline T min(T x, T y) { return std::min(x, y); } \
LOL_ATTR_NODISCARD static inline T max(T x, T y) { return std::max(x, y); } \
LOL_ATTR_NODISCARD static inline T clamp(T x, T y, T z) { return min(max(x, y), z); } \
LOL_ATTR_NODISCARD static inline T saturate(T x) { return min(max(x, (T)0), (T)1); }
LOL_ATTR_NODISCARD static inline T saturate(T x) { return min(max(x, (T)0), (T)1); } \
LOL_ATTR_NODISCARD static inline T gcd(T x, T y) { return y == (T)0 ? lol::abs(x) : lol::gcd(y, lol::fmod(x, y)); }

#define LOL_GENERIC_FUNC_SIGNED(T) \
LOL_GENERIC_FUNC(T) \


+ 1
- 1
src/lol/math/half.h View File

@@ -24,7 +24,7 @@
namespace lol
{

/* This is OUR namespace. Don't let Windows headers fuck with it. */
/* This is OUR namespace. Don't let Windows headers mess with it. */
#undef min
#undef max



+ 1
- 1
src/lol/math/real.h View File

@@ -24,7 +24,7 @@
namespace lol
{

/* This is OUR namespace. Don't let Windows headers fuck with it. */
/* This is OUR namespace. Don't let Windows headers mess with it. */
#undef min
#undef max



+ 1
- 1
src/t/Makefile.am View File

@@ -37,7 +37,7 @@ test_math_SOURCES = test-common.cpp \
math/cmplx.cpp math/half.cpp math/interp.cpp math/matrix.cpp \
math/quat.cpp math/rand.cpp math/real.cpp math/rotation.cpp \
math/trig.cpp math/vector.cpp math/polynomial.cpp math/noise/simplex.cpp \
math/bigint.cpp math/sqt.cpp
math/bigint.cpp math/sqt.cpp math/numbers.cpp
test_math_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/tools/lolunit
test_math_DEPENDENCIES = @LOL_DEPS@



+ 44
- 0
src/t/math/numbers.cpp View File

@@ -0,0 +1,44 @@
//
// Lol Engine — Unit tests for number theory
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What the Fuck You Want
// to Public License, Version 2, as published by the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//

#include <lol/engine-internal.h>

#include <lolunit.h>

namespace lol
{

lolunit_declare_fixture(gcd_test)
{
lolunit_declare_test(gcd_int)
{
lolunit_assert_equal(1, lol::gcd(37, 600));
lolunit_assert_equal(2, lol::gcd(4, 6));
lolunit_assert_equal(2, lol::gcd(6, 4));
lolunit_assert_equal(13, lol::gcd(13, 13));
lolunit_assert_equal(20, lol::gcd(20, 100));
lolunit_assert_equal(18913, lol::gcd(624129, 2061517));
}

lolunit_declare_test(gcd_negative)
{
lolunit_assert_equal(2, lol::gcd(4, -6));
lolunit_assert_equal(2, lol::gcd(-4, 6));
lolunit_assert_equal(2, lol::gcd(-4, -6));
lolunit_assert_equal(2, lol::gcd(6, -4));
lolunit_assert_equal(2, lol::gcd(-6, 4));
lolunit_assert_equal(2, lol::gcd(-6, -4));
}
};

} /* namespace lol */


+ 1
- 0
src/t/test-math.vcxproj View File

@@ -42,6 +42,7 @@
<ClCompile Include="math\interp.cpp" />
<ClCompile Include="math\matrix.cpp" />
<ClCompile Include="math\noise\simplex.cpp" />
<ClCompile Include="math\numbers.cpp" />
<ClCompile Include="math\polynomial.cpp" />
<ClCompile Include="math\quat.cpp" />
<ClCompile Include="math\rand.cpp" />


Loading…
Cancel
Save