Skip to content

Commit

Permalink
Always use 64-bit arithmetic (#245)
Browse files Browse the repository at this point in the history
* use always 64-bit arithemtics
long is only 32-bit on 32-bit systems and on 64-bit Windows
ensure we have consistent behavior

* Adding check for stdint.h to configure.ac and incuding stdint.h in CoinRational.hpp

---------

Co-authored-by: Ted Ralphs <[email protected]>
  • Loading branch information
christoph-cullmann and tkralphs authored Feb 17, 2025
1 parent 1678200 commit 328760e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
7 changes: 7 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -19563,6 +19563,13 @@ then :

fi

ac_fn_cxx_check_header_compile "$LINENO" "stdint.h" "ac_cv_header_stdint_h" "$ac_includes_default"
if test "x$ac_cv_header_stdint_h" = xyes
then :
printf "%s\n" "#define HAVE_STDINT_H 1" >>confdefs.h

fi


############################################################################
# Data type and math function definitions #
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ AC_COIN_DEBUGLEVEL
AC_CHECK_HEADERS([windows.h])
AC_CHECK_HEADERS([endian.h])
AC_CHECK_HEADERS([execinfo.h])
AC_CHECK_HEADERS([stdint.h])

############################################################################
# Data type and math function definitions #
Expand Down
11 changes: 8 additions & 3 deletions src/CoinRational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright 2015, Matthew Saltzman and Ted Ralphs
// Licensed under the Eclipse Public License

#include "CoinUtilsConfig.h"

#include <algorithm>
#include <cmath>
#ifdef __clang__
Expand All @@ -10,6 +12,9 @@
#include <cstdio>
#endif
#include <cassert>
#ifdef COINUTILS_HAS_STDINT_H
#include <stdint.h>
#endif

#include "CoinRational.hpp"

Expand All @@ -20,7 +25,7 @@
// Returns closest (or almost, anyway) rational to val with denominator less
// than or equal to maxdnom. Return value is true if within tolerance, false
// otherwise.
bool CoinRational::nearestRational_(double val, double maxdelta, long maxdnom)
bool CoinRational::nearestRational_(double val, double maxdelta, int64_t maxdnom)
{
double intpart;
if (floor(val)==val) {
Expand All @@ -31,7 +36,7 @@ bool CoinRational::nearestRational_(double val, double maxdelta, long maxdnom)
double fracpart = fabs(modf(val, &intpart));
// Consider using remainder() instead?

long a = 0, b = 1, c = 1, d = 1;
int64_t a = 0, b = 1, c = 1, d = 1;
#define DEBUG_X 1
#if DEBUG_X
bool shouldBeOK = false;
Expand Down Expand Up @@ -83,7 +88,7 @@ bool CoinRational::nearestRational_(double val, double maxdelta, long maxdnom)
numerator_ *= -1;
#if DEBUG_X > 1
if (shouldBeOK) {
printf("val %g is %ld/%ld to accuracy %g\n", val, numerator_, denominator_,
printf("val %g is %lld/%lld to accuracy %g\n", val, numerator_, denominator_,
fabs(val - numerator_ / double(denominator_)));
}
#endif
Expand Down
14 changes: 7 additions & 7 deletions src/CoinRational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ class COINUTILSLIB_EXPORT CoinRational
{

public:
long getDenominator() { return denominator_; }
long getNumerator() { return numerator_; }
int64_t getDenominator() { return denominator_; }
int64_t getNumerator() { return numerator_; }

CoinRational()
: numerator_(0)
, denominator_(1) {};

CoinRational(long n, long d)
CoinRational(int64_t n, int64_t d)
: numerator_(n)
, denominator_(d) {};

CoinRational(double val, double maxdelta, long maxdnom)
CoinRational(double val, double maxdelta, int64_t maxdnom)
{
if (!nearestRational_(val, maxdelta, maxdnom)) {
numerator_ = 0;
Expand All @@ -34,10 +34,10 @@ class COINUTILSLIB_EXPORT CoinRational
};

private:
long numerator_;
long denominator_;
int64_t numerator_;
int64_t denominator_;

bool nearestRational_(double val, double maxdelta, long maxdnom);
bool nearestRational_(double val, double maxdelta, int64_t maxdnom);
};

#endif
Expand Down

0 comments on commit 328760e

Please sign in to comment.