Skip to content

Commit

Permalink
Fix remainder and cremainder for integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
bunelr authored and soumith committed Apr 8, 2017
1 parent 56e3a5a commit 7d9217c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/TH/generic/THTensorMath.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,19 @@ void THTensor_(remainder)(THTensor *r_, THTensor *t, real value)
#if defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE)
rp[i] = (value == 0)? NAN : tp[i] - value * floor(tp[i] / value);
#else
rp[i] = tp[i] - value * (tp[i] / value); // There is no NAN for integers
// There is no NAN for integers
rp[i] = tp[i] % value;
if (rp[i] * value < 0)
rp[i] += value;
#endif
}
} else {
#if defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE)
TH_TENSOR_APPLY2(real, r_, real, t, *r__data = (value == 0)? NAN : *t_data - value * floor(*t_data / value););
#else
// There is no NAN for integers
TH_TENSOR_APPLY2(real, r_, real, t, *r__data = *t_data - value * (*t_data / value););
TH_TENSOR_APPLY2(real, r_, real, t, *r__data = *t_data % value;
if (*r__data * value < 0) *r__data += value;);
#endif
}
}
Expand Down Expand Up @@ -991,15 +995,19 @@ void THTensor_(cremainder)(THTensor *r_, THTensor *t, THTensor *src)
#if defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE)
rp[i] = (sp[i] == 0)? NAN : tp[i] - sp[i] * floor(tp[i] / sp[i]);
#else
rp[i] = tp[i] - sp[i] * (tp[i] / sp[i]); // There is no NAN for integers
// There is no NAN for integers
rp[i] = tp[i] % sp[i];
if (rp[i] * sp[i] < 0)
rp[i] += sp[i];
#endif
}
} else {
#if defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE)
TH_TENSOR_APPLY3(real, r_, real, t, real, src, *r__data = (*src_data == 0)? NAN : *t_data - *src_data * floor(*t_data / *src_data););
#else
// There is no NAN for integers
TH_TENSOR_APPLY3(real, r_, real, t, real, src, *r__data = *t_data - *src_data * (*t_data / *src_data););
TH_TENSOR_APPLY3(real, r_, real, t, real, src, *r__data = *t_data % *src_data;
if (*r__data * *src_data < 0) *r__data += *src_data;);
#endif

}
Expand Down

0 comments on commit 7d9217c

Please sign in to comment.