Merge branch 'lcm-overflow'

This commit is contained in:
Yuichi Nishiwaki 2014-03-25 12:27:12 +09:00
commit e74f700c51
3 changed files with 13 additions and 4 deletions

View File

@ -33,6 +33,7 @@ extern "C" {
#include <setjmp.h>
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <assert.h>
#include "xvect/xvect.h"

View File

@ -156,6 +156,8 @@ typedef struct pic_blob pic_blob;
static inline enum pic_tt pic_type(pic_value);
static inline const char *pic_type_repr(enum pic_tt);
static inline bool pic_valid_int(double);
static inline pic_value pic_nil_value();
static inline pic_value pic_true_value();
static inline pic_value pic_false_value();
@ -256,6 +258,12 @@ pic_type_repr(enum pic_tt tt)
return 0; /* logic flaw */
}
static inline bool
pic_valid_int(double v)
{
return INT_MIN <= v && v <= INT_MAX;
}
static inline pic_value
pic_nil_value()
{

View File

@ -20,10 +20,10 @@ gcd(int a, int b)
return b;
}
static int
static double
lcm(int a, int b)
{
return abs(a * b) / gcd(a, b);
return fabs((double)a * b) / gcd(a, b);
}
static pic_value
@ -439,7 +439,7 @@ pic_number_lcm(pic_state *pic)
{
size_t argc;
pic_value *args;
int r;
double r;
bool e = true;
pic_get_args(pic, "*", &argc, &args);
@ -457,7 +457,7 @@ pic_number_lcm(pic_state *pic)
pic_error(pic, "lcm: number required");
}
}
return e ? pic_int_value(r) : pic_float_value(r);
return e && pic_valid_int(r) ? pic_int_value(r) : pic_float_value(r);
}
static pic_value