go-overflow
is a lightweight Go library designed to handle integer arithmetic operations while checking for overflow and underflow conditions. It provides utility functions for addition, subtraction, multiplication, and division for various integer types (int8
, int16
, int32
, int64
, uint8
, uint16
, uint32
, uint64
).
- Overflow Detection: Detects overflow during addition, subtraction, and multiplication.
- Underflow Detection: Detects underflow during subtraction.
- Division Safety: Handles division by zero and overflow during division.
- Supports both signed and unsigned integer types.
To use go-overflow
in your project, simply run:
go get github.com/mrtkp9993/go-overflow
Import the library in your Go project:
import "github.com/mrtkp9993/go-overflow"
result, overflow := overflow.AddInt8(120, 10)
if overflow {
fmt.Println("Overflow occurred!")
} else {
fmt.Println("Result:", result)
}
result, underflow := overflow.SubInt16(10, 20)
if underflow {
fmt.Println("Underflow occurred!")
} else {
fmt.Println("Result:", result)
}
result, overflow := overflow.MulInt32(100000, 100000)
if overflow {
fmt.Println("Overflow occurred!")
} else {
fmt.Println("Result:", result)
}
result, errorOccurred := overflow.DivInt64(-9223372036854775808, -1)
if errorOccurred {
fmt.Println("Error occurred during division!")
} else {
fmt.Println("Result:", result)
}
AddInt8
,AddInt16
,AddInt32
,AddInt64
SubInt8
,SubInt16
,SubInt32
,SubInt64
MulInt8
,MulInt16
,MulInt32
,MulInt64
DivInt8
,DivInt16
,DivInt32
,DivInt64
AddUint8
,AddUint16
,AddUint32
,AddUint64
SubUint8
,SubUint16
,SubUint32
,SubUint64
MulUint8
,MulUint16
,MulUint32
,MulUint64
Contributions are welcome! Feel free to submit issues or pull requests to improve the library.
This project is licensed under the GNU GPLv3 License. See the LICENSE
file for details.