Skip to content

Commit

Permalink
* marshal.c (div0), numeric.c (infinite_value): new functions to
Browse files Browse the repository at this point in the history
  get rid of VC divion by 0 warnings.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Mar 12, 2009
1 parent 1c6ca1e commit 080525a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
5 changes: 4 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Thu Mar 12 18:02:05 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
Thu Mar 12 18:09:14 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>

* marshal.c (div0), numeric.c (infinite_value): new functions to
get rid of VC divion by 0 warnings.

* st.c: use st_index_t for indexes instead of int.

Expand Down
22 changes: 18 additions & 4 deletions marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,20 @@ obj_alloc_by_path(const char *path, struct load_arg *arg)
return rb_obj_alloc(klass);
}

#if defined _MSC_VER && _MSC_VER >= 1300
#pragma warning(push)
#pragma warning(disable:4723)
#endif
static double
div0(double x)
{
double t = 0.0;
return x / t;
}
#if defined _MSC_VER && _MSC_VER >= 1300
#pragma warning(pop)
#endif

static VALUE
r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
{
Expand Down Expand Up @@ -1292,18 +1306,18 @@ r_object0(struct load_arg *arg, int *ivp, VALUE extmod)

case TYPE_FLOAT:
{
double d, t = 0.0;
double d;
VALUE str = r_bytes(arg);
const char *ptr = RSTRING_PTR(str);

if (strcmp(ptr, "nan") == 0) {
d = t / t;
d = div0(0.0);
}
else if (strcmp(ptr, "inf") == 0) {
d = 1.0 / t;
d = div0(+1.0);
}
else if (strcmp(ptr, "-inf") == 0) {
d = -1.0 / t;
d = div0(-1.0);
}
else {
char *e;
Expand Down
19 changes: 16 additions & 3 deletions numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,20 @@ int_pow(long x, unsigned long y)
return LONG2NUM(z);
}

#if defined _MSC_VER && _MSC_VER >= 1300
#pragma warning(push)
#pragma warning(disable:4723)
#endif
static inline double
infinite_value(void)
{
static const double zero = 0.0;
return 1.0 / zero;
}
#if defined _MSC_VER && _MSC_VER >= 1300
#pragma warning(pop)
#endif

/*
* call-seq:
* fix ** other => Numeric
Expand All @@ -2460,7 +2474,6 @@ int_pow(long x, unsigned long y)
static VALUE
fix_pow(VALUE x, VALUE y)
{
static const double zero = 0.0;
long a = FIX2LONG(x);

if (FIXNUM_P(y)) {
Expand All @@ -2473,7 +2486,7 @@ fix_pow(VALUE x, VALUE y)
if (b == 1) return x;
if (a == 0) {
if (b > 0) return INT2FIX(0);
return DBL2NUM(1.0 / zero);
return DBL2NUM(infinite_value());
}
if (a == 1) return INT2FIX(1);
if (a == -1) {
Expand Down Expand Up @@ -2501,7 +2514,7 @@ fix_pow(VALUE x, VALUE y)
case T_FLOAT:
if (RFLOAT_VALUE(y) == 0.0) return DBL2NUM(1.0);
if (a == 0) {
return DBL2NUM(RFLOAT_VALUE(y) < 0 ? (1.0 / zero) : 0.0);
return DBL2NUM(RFLOAT_VALUE(y) < 0 ? infinite_value() : 0.0);
}
if (a == 1) return DBL2NUM(1.0);
return DBL2NUM(pow((double)a, RFLOAT_VALUE(y)));
Expand Down

0 comments on commit 080525a

Please sign in to comment.