Skip to content

Commit 328760e

Browse files
Always use 64-bit arithmetic (#245)
* 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]>
1 parent 1678200 commit 328760e

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

configure

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19563,6 +19563,13 @@ then :
1956319563

1956419564
fi
1956519565

19566+
ac_fn_cxx_check_header_compile "$LINENO" "stdint.h" "ac_cv_header_stdint_h" "$ac_includes_default"
19567+
if test "x$ac_cv_header_stdint_h" = xyes
19568+
then :
19569+
printf "%s\n" "#define HAVE_STDINT_H 1" >>confdefs.h
19570+
19571+
fi
19572+
1956619573

1956719574
############################################################################
1956819575
# Data type and math function definitions #

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ AC_COIN_DEBUGLEVEL
5252
AC_CHECK_HEADERS([windows.h])
5353
AC_CHECK_HEADERS([endian.h])
5454
AC_CHECK_HEADERS([execinfo.h])
55+
AC_CHECK_HEADERS([stdint.h])
5556

5657
############################################################################
5758
# Data type and math function definitions #

src/CoinRational.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Copyright 2015, Matthew Saltzman and Ted Ralphs
33
// Licensed under the Eclipse Public License
44

5+
#include "CoinUtilsConfig.h"
6+
57
#include <algorithm>
68
#include <cmath>
79
#ifdef __clang__
@@ -10,6 +12,9 @@
1012
#include <cstdio>
1113
#endif
1214
#include <cassert>
15+
#ifdef COINUTILS_HAS_STDINT_H
16+
#include <stdint.h>
17+
#endif
1318

1419
#include "CoinRational.hpp"
1520

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

34-
long a = 0, b = 1, c = 1, d = 1;
39+
int64_t a = 0, b = 1, c = 1, d = 1;
3540
#define DEBUG_X 1
3641
#if DEBUG_X
3742
bool shouldBeOK = false;
@@ -83,7 +88,7 @@ bool CoinRational::nearestRational_(double val, double maxdelta, long maxdnom)
8388
numerator_ *= -1;
8489
#if DEBUG_X > 1
8590
if (shouldBeOK) {
86-
printf("val %g is %ld/%ld to accuracy %g\n", val, numerator_, denominator_,
91+
printf("val %g is %lld/%lld to accuracy %g\n", val, numerator_, denominator_,
8792
fabs(val - numerator_ / double(denominator_)));
8893
}
8994
#endif

src/CoinRational.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ class COINUTILSLIB_EXPORT CoinRational
1414
{
1515

1616
public:
17-
long getDenominator() { return denominator_; }
18-
long getNumerator() { return numerator_; }
17+
int64_t getDenominator() { return denominator_; }
18+
int64_t getNumerator() { return numerator_; }
1919

2020
CoinRational()
2121
: numerator_(0)
2222
, denominator_(1) {};
2323

24-
CoinRational(long n, long d)
24+
CoinRational(int64_t n, int64_t d)
2525
: numerator_(n)
2626
, denominator_(d) {};
2727

28-
CoinRational(double val, double maxdelta, long maxdnom)
28+
CoinRational(double val, double maxdelta, int64_t maxdnom)
2929
{
3030
if (!nearestRational_(val, maxdelta, maxdnom)) {
3131
numerator_ = 0;
@@ -34,10 +34,10 @@ class COINUTILSLIB_EXPORT CoinRational
3434
};
3535

3636
private:
37-
long numerator_;
38-
long denominator_;
37+
int64_t numerator_;
38+
int64_t denominator_;
3939

40-
bool nearestRational_(double val, double maxdelta, long maxdnom);
40+
bool nearestRational_(double val, double maxdelta, int64_t maxdnom);
4141
};
4242

4343
#endif

0 commit comments

Comments
 (0)