Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: math: overflow detecting arithmetic #60858

Closed
Verseth opened this issue Jun 17, 2023 · 3 comments
Closed

proposal: math: overflow detecting arithmetic #60858

Verseth opened this issue Jun 17, 2023 · 3 comments
Labels
Milestone

Comments

@Verseth
Copy link

Verseth commented Jun 17, 2023

Currently there is no standard, efficient way of detecting overflow/underflow in integer arithmetic.

Everyone has to devise their own functions and algorithms for checking if an overflow/underflow happened, which is tedious and likely to be inefficient.

I propose a new suite of functions in the math package.
Each one would return the result together with a boolean flag set to false when an overflow/underflow took place.

func AddOverflow[T int | uint | int64 | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](i T) (T, bool)

Add two integers and detect overflow.

func SubOverflow[T int | uint | int64 | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](i T) (T, bool)

Subtract two integers and detect overflow.

func MulOverflow[T int | uint | int64 | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](i T) (T, bool)

Multiply two integers and detect overflow.

func DivOverflow[T int | uint | int64 | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](i T) (T, bool)

Divide two integers and detect overflow.

Example usage:

import "math"

var a int8 = 127
var b int8 = 1
c, ok := math.AddOverflow(a, b)
//  c = -128
// ok = false

a = 25
b = 4
c, ok = math.AddOverflow(a, b)
//  c = 29
// ok = true

Another option would be to enhance the arithmetic operators to optionally allow two return values.
This however would constitute a language change.

var a int8 = 127
var b int8 = 1
c, ok := a + b
//  c = -128
// ok = false
@gopherbot gopherbot added this to the Proposal milestone Jun 17, 2023
@Verseth Verseth changed the title proposal: add overflow detecting arithmetic proposal: math: overflow detecting arithmetic Jun 17, 2023
@icholy
Copy link

icholy commented Jun 17, 2023

Integer division cannot overflow.

@Verseth
Copy link
Author

Verseth commented Jun 17, 2023

@icholy Consider the following example

var a int8 = -128
var b int8 = -1
fmt.Println(a / b)
// -128

@randall77
Copy link
Contributor

Closing as dup of #24853, and related to #30209 and #32063. Some of these functions are easily simulateable with math/bits operations.

Integer division can overflow, in the very special case of math.MinInt / -1.
And arguably, /0 also. Presumably DivOverflow would return false and not panic if dividing by 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants