complex.c: sign of zeros
* complex.c (rb_nucomp_mul): preserve sign of zeros without NaN by regularized values. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
694d6f27cd
commit
41b2f1ada4
38
complex.c
38
complex.c
@ -19,6 +19,10 @@
|
|||||||
#define ONE INT2FIX(1)
|
#define ONE INT2FIX(1)
|
||||||
#define TWO INT2FIX(2)
|
#define TWO INT2FIX(2)
|
||||||
#define RFLOAT_0 DBL2NUM(0)
|
#define RFLOAT_0 DBL2NUM(0)
|
||||||
|
#if defined(HAVE_SIGNBIT) && defined(__GNUC__) && defined(__sun) && \
|
||||||
|
!defined(signbit)
|
||||||
|
extern int signbit(double);
|
||||||
|
#endif
|
||||||
|
|
||||||
VALUE rb_cComplex;
|
VALUE rb_cComplex;
|
||||||
|
|
||||||
@ -739,6 +743,19 @@ nucomp_sub(VALUE self, VALUE other)
|
|||||||
return f_addsub(self, other, f_sub, '-');
|
return f_addsub(self, other, f_sub, '-');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
safe_mul(VALUE a, VALUE b, int az, int bz)
|
||||||
|
{
|
||||||
|
double v;
|
||||||
|
if (!az && bz && RB_FLOAT_TYPE_P(a) && !isnan(v = RFLOAT_VALUE(a))) {
|
||||||
|
a = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
|
||||||
|
}
|
||||||
|
if (!bz && az && RB_FLOAT_TYPE_P(b) && !isnan(v = RFLOAT_VALUE(b))) {
|
||||||
|
b = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
|
||||||
|
}
|
||||||
|
return f_mul(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* cmp * numeric -> complex
|
* cmp * numeric -> complex
|
||||||
@ -757,17 +774,18 @@ rb_nucomp_mul(VALUE self, VALUE other)
|
|||||||
if (k_complex_p(other)) {
|
if (k_complex_p(other)) {
|
||||||
VALUE real, imag;
|
VALUE real, imag;
|
||||||
VALUE areal, aimag, breal, bimag;
|
VALUE areal, aimag, breal, bimag;
|
||||||
|
int arzero, aizero, brzero, bizero;
|
||||||
|
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
if (f_zero_p(areal = adat->real)) areal = ZERO;
|
arzero = !!f_zero_p(areal = adat->real);
|
||||||
if (f_zero_p(aimag = adat->imag)) aimag = ZERO;
|
aizero = !!f_zero_p(aimag = adat->imag);
|
||||||
if (f_zero_p(breal = bdat->real)) breal = ZERO;
|
brzero = !!f_zero_p(breal = bdat->real);
|
||||||
if (f_zero_p(bimag = bdat->imag)) bimag = ZERO;
|
bizero = !!f_zero_p(bimag = bdat->imag);
|
||||||
real = (areal == ZERO || breal == ZERO) ? ZERO : f_mul(areal, breal);
|
real = f_sub(safe_mul(areal, breal, arzero, brzero),
|
||||||
if (aimag != ZERO && bimag != ZERO) real = f_sub(real, f_mul(aimag, bimag));
|
safe_mul(aimag, bimag, aizero, bizero));
|
||||||
imag = (areal == ZERO || bimag == ZERO) ? ZERO : f_mul(areal, bimag);
|
imag = f_add(safe_mul(areal, bimag, arzero, bizero),
|
||||||
if (aimag != ZERO && breal != ZERO) imag = f_add(imag, f_mul(aimag, breal));
|
safe_mul(aimag, breal, aizero, brzero));
|
||||||
|
|
||||||
return f_complex_new2(CLASS_OF(self), real, imag);
|
return f_complex_new2(CLASS_OF(self), real, imag);
|
||||||
}
|
}
|
||||||
@ -1238,10 +1256,6 @@ nucomp_eql_p(VALUE self, VALUE other)
|
|||||||
inline static VALUE
|
inline static VALUE
|
||||||
f_signbit(VALUE x)
|
f_signbit(VALUE x)
|
||||||
{
|
{
|
||||||
#if defined(HAVE_SIGNBIT) && defined(__GNUC__) && defined(__sun) && \
|
|
||||||
!defined(signbit)
|
|
||||||
extern int signbit(double);
|
|
||||||
#endif
|
|
||||||
if (RB_TYPE_P(x, T_FLOAT)) {
|
if (RB_TYPE_P(x, T_FLOAT)) {
|
||||||
double f = RFLOAT_VALUE(x);
|
double f = RFLOAT_VALUE(x);
|
||||||
return f_boolcast(!isnan(f) && signbit(f));
|
return f_boolcast(!isnan(f) && signbit(f));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user