Robbepop / apint

Arbitrary precision integers library.
Other
27 stars 4 forks source link

Error when passing {integer}::MIN to any of the resizing functions #15

Closed AaronKutch closed 6 years ago

AaronKutch commented 6 years ago
extern crate apint;
use apint::{ApInt, BitWidth};
use std::i128;

fn main() {
    println!(
        "{:?}",
        ApInt::from_i128(i128::MAX).into_sign_extend(BitWidth::new(256).unwrap()),
    );
    println!(
        "{:?}",
        ApInt::from_i128(i128::MIN).into_sign_extend(BitWidth::new(256).unwrap()),
    );
}

If I were you, I would add tests for the problematic {integer}::MIN values everywhere applicable.

Also, I found this in the documentation:

pub fn from_i128(val: i128) -> ApInt
[src]
[−]
Creates a new ApInt from a given i128 value with a bit-width of 64.
pub fn from_u128(val: u128) -> ApInt
[src]
[−]
Creates a new ApInt from a given u128 value with a bit-width of 64.

It should be 128 not 64.

Also, now that i128 is stabilized (except for repr(i128)), check that the implementation and docs of all the i128 stuff is also correct.

This crate perfectly fits my use case (and I think that it is much, much better than the num_bigint crate in general), but there is some unimplemented! stuff in the multiply and divide functions that prevents me from using it.

Robbepop commented 6 years ago

Thank you for your bug report! I will look into this and comment more thoroughly tomorrow - it is very late here. ;)

AaronKutch commented 6 years ago

My application relies on there being no edge cases or unexpected behavior, so you can expect more in the future. Do have plans for fixing the unimplemented parts in the next month or so? I can perform inefficient mult and div functions on my side so actually I can start using the crate right away. I am using your crate to make large integers with behaviors just like the primitives. Also, can you make a function that returns the bit width of the ApInt? It would make some things easier for me.

Robbepop commented 6 years ago

You can get the bit width of an ApInt, Int or Uint instance by using the Width trait with its width method. For this, however, you have to explicitely import (use) it with use apint::Width; otherwise it is not visible to you. This is unergonomic and might improve in future versions.

I am using the apint crate myself in stevia so it is also important for my other project that the apint crate improves. :)

Robbepop commented 6 years ago

I will try to fix the reported bugs in the apint crate next weekend (maybe even earlier) and also try to implement multiplication and division routines for large apint instances as well. However, PRs are generally welcome, too. :)

Robbepop commented 6 years ago

I have fixed the reported bug in version 0.1.0 of the crate.