ramp icon indicating copy to clipboard operation
ramp copied to clipboard

Shr mismatches primitives integers for negative values

Open huonw opened this issue 10 years ago • 1 comments
trafficstars

>>'ing a primitive integer (or a GMP Mpz) will round to negative infinity, while ramp currently rounds to zero (same as division). Maybe this should be changed?

extern crate ramp;
extern crate gmp;
use ramp::Int;

fn main() {
    let x = -1;
    let shift = 1;

    let r = Int::from(x);
    let g = gmp::mpz::Mpz::from(x);
    let div = 1i32 << shift;
    println!("ramp {} {}\ngmp  {} {}\ni32  {} {}",
             &r >> shift,
             r / div,
             &g >> shift,
             g / div as u64,
             x >> shift,
             x / div);
}
ramp 0 0
gmp  -1 0
i32  -1 0

huonw avatar Oct 22 '15 05:10 huonw

Well right now it says that the bit-shift operators correspond to a multiplication or division by a power-of-two, so the results Ramp gets are correct. That said, I am using the same "pretend it's two's complement" thing for BitOr and BitAnd, so using for the shl/shr isn't too weird.

Aatch avatar Oct 22 '15 05:10 Aatch