Method: Calc::Numeric#sgn

Defined in:
ext/calc/numeric.c

#sgnCalc::C, Calc::Q

Indicates sign of a real or complex number

For real x, x.sgn returns

-1 if x < 0
 0 if x == 0
 1 if x > 0

For complex x, x.sgn returns Calc::C(x.re.sgn, x.im.sgn)

Examples:

Calc::Q(9).sgn     #=> Calc::Q(1)
Calc::Q(0).sgn     #=> Calc::Q(0)
Calc::Q(-9).sgn    #=> Calc::Q(-1)
Calc::C(1, -1).sgn #=> Calc::C(1-1i)

Returns:



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'ext/calc/numeric.c', line 524

static VALUE
cn_sgn(VALUE self)
{
    COMPLEX *cself, *cresult;
    setup_math_error();

    if (CALC_Q_P(self)) {
        return wrap_number(qsign(DATA_PTR(self)));
    }
    cself = DATA_PTR(self);
    cresult = comalloc();
    qfree(cresult->real);
    qfree(cresult->imag);
    cresult->real = qsign(cself->real);
    cresult->imag = qsign(cself->imag);
    return wrap_complex(cresult);
}