Hello Ziguanas. Seing we’re waiting for ranged integers, I’m implememting them in userland: bint.
For now they provide basic arithmetic operations such as:
add,
sub,
mul,
div with .floor or .trunc,
min,
max,
ceil,
floor,
closest,
furthest,
ord,
Those operations, instead of saturating, wrapping, panicking or triggering UB, are widening (and narrowing): they return a large enough type to never overflow/underflow.
For those interested in learning more, I suggest you to read the doc-tests which are written a bit like a tutorial by example.
I’m curious to get your feedback, what would you want from ranged integers?
5 Likes
Nice, this is a great way to play with this concept before we get ranged integers.
I had a few thoughts.
I was a little confused as to why you went with widening/narrowing instead of the other options. Your readme helped explain it:
In short, if an overflow/underflow is even possible, the program won’t compile.
This is clever, in a good sense. Allowing the compiler to step in even though this isn’t part of the core language.
One thing I would like, though, is to have the option of saturating/wrapping as that is desireable behavior sometimes. Since add/sub/mul are already function calls, you could add an enum like with div to allow for saturating/wrapping behavior.
It would be great if these had an iterator that you could get from it so you can use it with while since there’s not really a way to do it with for:
I see now that there is a Range type that does this, but there were no tests showing it so I had to look at the code. Either way, it would be good to get a range from a Bint that you can iterate over in a while loop
I’m going to implement both your suggestions eventually.
The ranges aren’t really part of the API, they’re mostly for me to test things. That’s why I don’t include them in the doc. However an iterator over a bint type seems useful, so I’ll expose and document one soon. Thank you for suggesting it.
Regarding other behaviors, I’ll implement them but I’m not sure how I’ll expose them. The types might be different enough that I want different functions altogether. But it’ll come.
1 Like