Get rid of redundant type checks.
Define % operator similar to int%int.
This commit is contained in:
parent
5ad58c6aef
commit
56cd67ad47
|
@ -139,86 +139,70 @@ float_compare(v, w)
|
||||||
static object *
|
static object *
|
||||||
float_add(v, w)
|
float_add(v, w)
|
||||||
floatobject *v;
|
floatobject *v;
|
||||||
object *w;
|
floatobject *w;
|
||||||
{
|
{
|
||||||
if (!is_floatobject(w)) {
|
return newfloatobject(v->ob_fval + w->ob_fval);
|
||||||
err_badarg();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return newfloatobject(v->ob_fval + ((floatobject *)w) -> ob_fval);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
float_sub(v, w)
|
float_sub(v, w)
|
||||||
floatobject *v;
|
floatobject *v;
|
||||||
object *w;
|
floatobject *w;
|
||||||
{
|
{
|
||||||
if (!is_floatobject(w)) {
|
return newfloatobject(v->ob_fval - w->ob_fval);
|
||||||
err_badarg();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return newfloatobject(v->ob_fval - ((floatobject *)w) -> ob_fval);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
float_mul(v, w)
|
float_mul(v, w)
|
||||||
floatobject *v;
|
floatobject *v;
|
||||||
object *w;
|
floatobject *w;
|
||||||
{
|
{
|
||||||
if (!is_floatobject(w)) {
|
return newfloatobject(v->ob_fval * w->ob_fval);
|
||||||
err_badarg();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return newfloatobject(v->ob_fval * ((floatobject *)w) -> ob_fval);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
float_div(v, w)
|
float_div(v, w)
|
||||||
floatobject *v;
|
floatobject *v;
|
||||||
object *w;
|
floatobject *w;
|
||||||
{
|
{
|
||||||
if (!is_floatobject(w)) {
|
if (w->ob_fval == 0) {
|
||||||
err_badarg();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (((floatobject *)w) -> ob_fval == 0) {
|
|
||||||
err_setstr(ZeroDivisionError, "float division");
|
err_setstr(ZeroDivisionError, "float division");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return newfloatobject(v->ob_fval / ((floatobject *)w) -> ob_fval);
|
return newfloatobject(v->ob_fval / w->ob_fval);
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
float_rem(v, w)
|
float_rem(v, w)
|
||||||
floatobject *v;
|
floatobject *v;
|
||||||
object *w;
|
floatobject *w;
|
||||||
{
|
{
|
||||||
double wx;
|
double vx, wx;
|
||||||
if (!is_floatobject(w)) {
|
double div, mod;
|
||||||
err_badarg();
|
wx = w->ob_fval;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
wx = ((floatobject *)w) -> ob_fval;
|
|
||||||
if (wx == 0.0) {
|
if (wx == 0.0) {
|
||||||
err_setstr(ZeroDivisionError, "float remainder");
|
err_setstr(ZeroDivisionError, "float modulo");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return newfloatobject(fmod(v->ob_fval, wx));
|
vx = v->ob_fval;
|
||||||
|
mod = fmod(vx, wx);
|
||||||
|
div = (vx - mod) / wx;
|
||||||
|
if (wx*mod < 0) {
|
||||||
|
mod += wx;
|
||||||
|
div -= 1.0;
|
||||||
|
}
|
||||||
|
return newfloatobject(mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
float_divmod(v, w)
|
float_divmod(v, w)
|
||||||
floatobject *v;
|
floatobject *v;
|
||||||
object *w;
|
floatobject *w;
|
||||||
{
|
{
|
||||||
double vx, wx;
|
double vx, wx;
|
||||||
double div, mod;
|
double div, mod;
|
||||||
object *t;
|
object *t;
|
||||||
if (!is_floatobject(w)) {
|
wx = w->ob_fval;
|
||||||
err_badarg();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
wx = ((floatobject *)w) -> ob_fval;
|
|
||||||
if (wx == 0.0) {
|
if (wx == 0.0) {
|
||||||
err_setstr(ZeroDivisionError, "float divmod()");
|
err_setstr(ZeroDivisionError, "float divmod()");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -245,15 +229,11 @@ float_divmod(v, w)
|
||||||
static object *
|
static object *
|
||||||
float_pow(v, w)
|
float_pow(v, w)
|
||||||
floatobject *v;
|
floatobject *v;
|
||||||
object *w;
|
floatobject *w;
|
||||||
{
|
{
|
||||||
double iv, iw, ix;
|
double iv, iw, ix;
|
||||||
if (!is_floatobject(w)) {
|
|
||||||
err_badarg();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
iv = v->ob_fval;
|
iv = v->ob_fval;
|
||||||
iw = ((floatobject *)w)->ob_fval;
|
iw = w->ob_fval;
|
||||||
/* Sort out special cases here instead of relying on pow() */
|
/* Sort out special cases here instead of relying on pow() */
|
||||||
if (iw == 0.0)
|
if (iw == 0.0)
|
||||||
return newfloatobject(1.0); /* x**0 is 1, even 0**0 */
|
return newfloatobject(1.0); /* x**0 is 1, even 0**0 */
|
||||||
|
@ -347,10 +327,3 @@ typeobject Floattype = {
|
||||||
0, /*tp_as_sequence*/
|
0, /*tp_as_sequence*/
|
||||||
0, /*tp_as_mapping*/
|
0, /*tp_as_mapping*/
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
XXX This is not enough. Need:
|
|
||||||
- automatic casts for mixed arithmetic (3.1 * 4)
|
|
||||||
- mixed comparisons (!)
|
|
||||||
- look at other uses of ints that could be extended to floats
|
|
||||||
*/
|
|
||||||
|
|
Loading…
Reference in New Issue