Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup and test emp::Fraction #498

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions include/emp/math/Fraction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
#define EMP_MATH_FRACTION_HPP_INCLUDE

#include <cstdint>

#include "math.hpp"
#include <numeric>

namespace emp {

Expand All @@ -23,6 +22,7 @@ namespace emp {
int64_t num; // Numerator
int64_t denom; // Denominator

public:
void Reduce() {
if (denom == 0) return; // Undefined value!
if (num == 0) { denom = 1; return; } // Zero value!
Expand All @@ -32,16 +32,16 @@ namespace emp {
denom = -denom;
num = -num;
}
const uint64_t gcd = emp::GCD(num, denom);
const int64_t gcd = std::gcd(num, denom); // overflows if uint64_t
num /= gcd;
denom /= gcd;
}
public:

Fraction(int64_t in_num=0, int64_t in_denom=1) : num(in_num), denom(in_denom) { }
Fraction(const Fraction &) = default;

int64_t GetNumerator() const { return num; }
int64_t GetDenomonator() const { return denom; }
int64_t GetDenominator() const { return denom; }
};
}

Expand Down
66 changes: 66 additions & 0 deletions tests/math/Fraction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This file is part of Empirical, https://github.com/devosoft/Empirical
* Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* date: 2023
*/
/**
* @file
*/

#include "third-party/Catch/single_include/catch2/catch.hpp"

#include "emp/math/Fraction.hpp"

TEST_CASE("Test Fraction Constructor", "[math]") {
emp::Fraction frac1;
REQUIRE(frac1.GetNumerator() == 0);
REQUIRE(frac1.GetDenominator() == 1);
frac1.Reduce();
REQUIRE(frac1.GetNumerator() == 0);
REQUIRE(frac1.GetDenominator() == 1);

emp::Fraction frac2(5, 10);
REQUIRE(frac2.GetNumerator() == 5);
REQUIRE(frac2.GetDenominator() == 10);
frac2.Reduce();
REQUIRE(frac2.GetNumerator() == 1);
REQUIRE(frac2.GetDenominator() == 2);

emp::Fraction frac3(0, 10);
REQUIRE(frac3.GetNumerator() == 0);
REQUIRE(frac3.GetDenominator() == 10); // Zero numerator
frac3.Reduce();
REQUIRE(frac3.GetNumerator() == 0);
REQUIRE(frac3.GetDenominator() == 1);

emp::Fraction frac4(10, -5);
REQUIRE(frac4.GetNumerator() == 10); // Negative handling
REQUIRE(frac4.GetDenominator() == -5);
frac4.Reduce();
REQUIRE(frac4.GetNumerator() == -2);
REQUIRE(frac4.GetDenominator() == 1);

emp::Fraction frac5(-10, 5);
REQUIRE(frac5.GetNumerator() == -10); // Negative handling
REQUIRE(frac5.GetDenominator() == 5);
frac5.Reduce();
REQUIRE(frac5.GetNumerator() == -2);
REQUIRE(frac5.GetDenominator() == 1);

emp::Fraction frac6(-10, -5);
REQUIRE(frac6.GetNumerator() == -10); // Negative handling
REQUIRE(frac6.GetDenominator() == -5);
frac6.Reduce();
REQUIRE(frac6.GetNumerator() == 2);
REQUIRE(frac6.GetDenominator() == 1);

}

TEST_CASE("Test Zero Denominator", "[math]") {
emp::Fraction frac(10, 0);
REQUIRE(frac.GetNumerator() == 10);
REQUIRE(frac.GetDenominator() == 0);
frac.Reduce();
REQUIRE(frac.GetNumerator() == 10);
REQUIRE(frac.GetDenominator() == 0); // Expect no change for zero denominator
}
2 changes: 1 addition & 1 deletion tests/math/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TEST_NAMES = combos distances Distribution info_theory math random_utils Random sequence_utils spatial_stats stats Range RangeSet
TEST_NAMES = combos distances Distribution Fraction info_theory math random_utils Random sequence_utils spatial_stats stats Range RangeSet

# -O3 -Wl,--stack,8388608 -ftrack-macro-expansion=0
FLAGS = -std=c++20 -g -pthread -Wall -Wno-unused-function -Wno-unused-private-field -I../../include/ -I../../ -I../../third-party/cereal/include/ -DCATCH_CONFIG_MAIN
Expand Down
Loading