Go's handling of large numbers arithmetic!

I would like to engage with someone(s) into discussing the design decision of GO not to produce Errors for when a user does some arithmetic that involve large numbers (which overflows their underlying type).

CODE
LINK TO PLAYGROUND

Even though adding two very very large int64 yields an overflow, GO still give back an int64 which is not the right intended- Correct result for such an operation?
How is the trust relationship between the CPU/ALU and the GO Compiler and who should take charge here?
For a second i thought about opening an issue but then I realised that maybe there is a good reason for the go team to leave it up to the user to know what they are doing!
Thanks in advance!

1 Like

You have to make a choice, cause a panic in case of overflow or wrap silently. If the choice is to panic in case of overflow, a test of overflow must be added after every arithmetic operation. This would slow down program execution. Unexpected overflows are very infrequent. The overhead is not worth it.

So I assume it is because of performance that the language designers chose to silently wrap and not panic in case of overflow.

2 Likes

Thank you for your input. Brief and concise!

Handling such overflow is as simple as checking wether the outcome of the operation is smaller than the biggest operand! Thank you for your input

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.