commit 70e87112d3aa47380efff102729ce317139072c7
parent bf4f1011deb30136bfb694ee55cca0b984f02f8f
Author: Andres Navarro <canavarro82@gmail.com>
Date: Wed, 6 Apr 2011 11:42:14 -0300
Bugfix: klcm32_64 was dividing by gcd twice instead of once.
Diffstat:
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/src/kghelpers.c b/src/kghelpers.c
@@ -295,8 +295,8 @@ int64_t kgcd32_64(int32_t a_, int32_t b_)
int64_t klcm32_64(int32_t a_, int32_t b_)
{
int64_t gcd = kgcd32_64(a_, b_);
- int64_t a = kabs64(a_) / gcd;
- int64_t b = kabs64(b_) / gcd;
-
- return a * b;
+ int64_t a = kabs64(a_);
+ int64_t b = kabs64(b_);
+ /* divide first to avoid possible overflow */
+ return (a / gcd) * b;
}
diff --git a/src/kghelpers.h b/src/kghelpers.h
@@ -409,6 +409,7 @@ inline int32_t kabs32(int32_t a) { return a < 0? -a : a; }
inline int64_t kabs64(int64_t a) { return a < 0? -a : a; }
inline int32_t kmin32(int32_t a, int32_t b) { return a < b? a : b; }
inline int32_t kmax32(int32_t a, int32_t b) { return a > b? a : b; }
+
inline int32_t kcheck32(klisp_State *K, char *msg, int64_t i)
{
if (i > (int64_t) INT32_MAX || i < (int64_t) INT32_MIN) {