Re:Fast midpoint between two integers without overflow
by tsimionescu on Wednesday December 07, 2022 @12:31PM
(#33897298)
But you don't know the MSB at compile time in the general case.
Re:Fast midpoint between two integers without overflow
by Aaargh20318 on Wednesday December 07, 2022 @03:17PM
(#33899658)
> If you are adding 0(Int18)+1(Int18), you don't need 1(Int36)
No, you’d need an Int19. We were talking about statically typed languages, so you need to decide the type at compile-time. If you add two UInt16’s they could both contain up to 0xFFFF, you need 17 bits to store that answer. Basically, with every addition you need 1 more bit than the largest of the two (types, not values) you are adding together to prevent a potential overflow. It’s even worse for multiplication.
Re:Fast midpoint between two integers without overflow
by taeric on Wednesday December 07, 2022 @03:43PM
(#33899953)
Couldn't you have a constraining operation in there to assert that you have enough bits? You are right that we don't know if `a + b` would need more bits than either a or b. However, we could have an assert that allows us to ensure the static constraints are satisfied. And the type system could be used as a place to know where we haven't checked the constraint.
(Note that I'm not too clear how valuable this would be. Just asking why that isn't a valid path.)
Re:Fast midpoint between two integers without overflow
by IncRnd on Wednesday December 07, 2022 @04:07PM
(#33900258)
> No, you’d need an Int19.
No, that's completely false. You don't need an Int19 but an Int1.
Re:Fast midpoint between two integers without overflow
by Aaargh20318 on Wednesday December 07, 2022 @05:12PM
(#33901074)
How do you figure that you can store the result of adding 2 Int18s in an Int1 ? Remember, we’re talking about static types and you don’t know the values at compile time.
Not really. You only need to preserve according to the msb of each number. If you are adding 0(Int18)+1(Int18), you don't need 1(Int36) anymore than you need 1(Int18) or even 1(Int1).