Method: BigDecimal#hash

Defined in:
bigdecimal.c

#hashInteger

Returns the integer hash value for self.

Two instances of BigDecimal have the same hash value if and only if they have equal:

  • Sign.

  • Fractional part.

  • Exponent.

Returns:



751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# File 'bigdecimal.c', line 751

static VALUE
BigDecimal_hash(VALUE self)
{
    ENTER(1);
    Real *p;
    st_index_t hash;

    GUARD_OBJ(p, GetVpValue(self, 1));
    hash = (st_index_t)p->sign;
    /* hash!=2: the case for 0(1),NaN(0) or +-Infinity(3) is sign itself */
    if(hash == 2 || hash == (st_index_t)-2) {
        hash ^= rb_memhash(p->frac, sizeof(DECDIG)*p->Prec);
        hash += p->exponent;
    }
    return ST2FIX(hash);
}