jhpratt / deranged

Proof of concept ranged integers in Rust.
Apache License 2.0
38 stars 5 forks source link

wrapping add #9

Closed Easyoakland closed 1 year ago

Easyoakland commented 1 year ago

Adding methods for wrapping.

I don't know if there is a better way to implement these.

Currently only wrapping_add is implemented but I can work on the others if this is the correct way to go about it.

jhpratt commented 1 year ago

I'm surprised you managed to figure out the inner workings, as basically nothing is documented right now.

Wrapping, checked, and overflowing arithmetic is something that I wanted to implement for a while, and was thinking about recently.

I will take a look into this soon! I imagine it'll take some thinking on my end.

Easyoakland commented 1 year ago

The implementation panics if the number is signed and the range is too large. Ideally this could be fixed with methods like checked_add_unsigned but the if_signed macro doesn't seem to work inside the method body and the unsigned methods are not implemented for unsigned types.

Although there isn't an equivalent rem_euclid for unsigned arguments so maybe that technique won't work anyway.

jhpratt commented 1 year ago

checked_add_unsigned should work. It'll require the MSRV be bumped, but that's not an issue.

As to rem_euclid, I imagine it's possible to implement it manually. I would provide a function definition, but I don't have sufficient time at the moment.

Easyoakland commented 1 year ago

@jhpratt I think I finished the no_panic implementation with a valid explanation why the algorithm works. Feel free to check that it's actually valid.

jhpratt commented 1 year ago

Thanks, I'll check it out! Now you know why I was putting off implementing it myself 😅

jhpratt commented 1 year ago

Quick status update: Apologies for the delay! Between RustConf and some other things, I've been quite busy. I intend to look at this in the next day or two.

jhpratt commented 1 year ago

Thanks for the changes! Everything LGTM. Merging.