overflow

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2025 License: GPL-3.0 Imports: 0 Imported by: 0

README

go-overflow

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).

Features

  • 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.

Installation

To use go-overflow in your project, simply run:

go get github.com/mrtkp9993/go-overflow

Usage

Import the library in your Go project:

import "github.com/mrtkp9993/go-overflow"
Examples
Addition with Overflow Check
result, overflow := overflow.AddInt8(120, 10)
if overflow {
    fmt.Println("Overflow occurred!")
} else {
    fmt.Println("Result:", result)
}
Subtraction with Underflow Check
result, underflow := overflow.SubInt16(10, 20)
if underflow {
    fmt.Println("Underflow occurred!")
} else {
    fmt.Println("Result:", result)
}
Multiplication with Overflow Check
result, overflow := overflow.MulInt32(100000, 100000)
if overflow {
    fmt.Println("Overflow occurred!")
} else {
    fmt.Println("Result:", result)
}
Division with Safety Check
result, errorOccurred := overflow.DivInt64(-9223372036854775808, -1)
if errorOccurred {
    fmt.Println("Error occurred during division!")
} else {
    fmt.Println("Result:", result)
}

Supported Functions

Signed Integers
  • AddInt8, AddInt16, AddInt32, AddInt64
  • SubInt8, SubInt16, SubInt32, SubInt64
  • MulInt8, MulInt16, MulInt32, MulInt64
  • DivInt8, DivInt16, DivInt32, DivInt64
Unsigned Integers
  • AddUint8, AddUint16, AddUint32, AddUint64
  • SubUint8, SubUint16, SubUint32, SubUint64
  • MulUint8, MulUint16, MulUint32, MulUint64

Contributing

Contributions are welcome! Feel free to submit issues or pull requests to improve the library.

License

This project is licensed under the GNU GPLv3 License. See the LICENSE file for details.

Documentation

Overview

go-overflow is a simple library to check for integer overflow in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddInt16

func AddInt16(a, b int16) (int16, bool)

AddInt16 adds two int16 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func AddInt32

func AddInt32(a, b int32) (int32, bool)

AddInt32 adds two int32 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func AddInt64

func AddInt64(a, b int64) (int64, bool)

AddInt64 adds two int64 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func AddInt8

func AddInt8(a, b int8) (int8, bool)

AddInt8 adds two int8 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func AddUint16

func AddUint16(a, b uint16) (uint16, bool)

AddUint16 adds two uint16 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func AddUint32

func AddUint32(a, b uint32) (uint32, bool)

AddUint32 adds two uint32 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func AddUint64

func AddUint64(a, b uint64) (uint64, bool)

AddUint64 adds two uint64 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func AddUint8

func AddUint8(a, b uint8) (uint8, bool)

AddUint8 adds two uint8 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func DivInt16

func DivInt16(a, b int16) (int16, bool)

DivInt16 divides two int16 values and checks for division by zero or overflow. Returns the result and a boolean indicating if an error occurred. Integer division overflows in one specific case: dividing the smallest negative value for the data type (see Maximum and Minimum Values) by -1. That’s because the correct result, which is the corresponding positive number, does not fit (see Integer Overflow) in the same number of bits. Source: https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Division-and-Remainder.html

func DivInt32

func DivInt32(a, b int32) (int32, bool)

DivInt32 divides two int32 values and checks for division by zero or overflow. Returns the result and a boolean indicating if an error occurred. Integer division overflows in one specific case: dividing the smallest negative value for the data type (see Maximum and Minimum Values) by -1. That’s because the correct result, which is the corresponding positive number, does not fit (see Integer Overflow) in the same number of bits. Source: https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Division-and-Remainder.html

func DivInt64

func DivInt64(a, b int64) (int64, bool)

DivInt64 divides two int64 values and checks for division by zero or overflow. Returns the result and a boolean indicating if an error occurred. Integer division overflows in one specific case: dividing the smallest negative value for the data type (see Maximum and Minimum Values) by -1. That’s because the correct result, which is the corresponding positive number, does not fit (see Integer Overflow) in the same number of bits. Source: https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Division-and-Remainder.html

func DivInt8

func DivInt8(a, b int8) (int8, bool)

DivInt8 divides two int8 values and checks for division by zero or overflow. Returns the result and a boolean indicating if an error occurred. Integer division overflows in one specific case: dividing the smallest negative value for the data type (see Maximum and Minimum Values) by -1. That’s because the correct result, which is the corresponding positive number, does not fit (see Integer Overflow) in the same number of bits. Source: https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Division-and-Remainder.html

func MulInt16

func MulInt16(a, b int16) (int16, bool)

MulInt16 multiplies two int16 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func MulInt32

func MulInt32(a, b int32) (int32, bool)

MulInt32 multiplies two int32 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func MulInt64

func MulInt64(a, b int64) (int64, bool)

MulInt64 multiplies two int64 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func MulInt8

func MulInt8(a, b int8) (int8, bool)

MulInt8 multiplies two int8 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func MulUint16

func MulUint16(a, b uint16) (uint16, bool)

MulUint16 multiplies two uint16 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func MulUint32

func MulUint32(a, b uint32) (uint32, bool)

MulUint32 multiplies two uint32 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func MulUint64

func MulUint64(a, b uint64) (uint64, bool)

MulUint64 multiplies two uint64 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func MulUint8

func MulUint8(a, b uint8) (uint8, bool)

MulUint8 multiplies two uint8 values and checks for overflow. Returns the result and a boolean indicating if overflow occurred.

func SubInt16

func SubInt16(a, b int16) (int16, bool)

SubInt16 subtracts two int16 values and checks for underflow. Returns the result and a boolean indicating if underflow occurred.

func SubInt32

func SubInt32(a, b int32) (int32, bool)

SubInt32 subtracts two int32 values and checks for underflow. Returns the result and a boolean indicating if underflow occurred.

func SubInt64

func SubInt64(a, b int64) (int64, bool)

SubInt64 subtracts two int64 values and checks for underflow. Returns the result and a boolean indicating if underflow occurred.

func SubInt8

func SubInt8(a, b int8) (int8, bool)

SubInt8 subtracts two int8 values and checks for underflow. Returns the result and a boolean indicating if underflow occurred.

func SubUint16

func SubUint16(a, b uint16) (uint16, bool)

SubUint16 subtracts two uint16 values and checks for underflow. Returns the result and a boolean indicating if underflow occurred.

func SubUint32

func SubUint32(a, b uint32) (uint32, bool)

SubUint32 subtracts two uint32 values and checks for underflow. Returns the result and a boolean indicating if underflow occurred.

func SubUint64

func SubUint64(a, b uint64) (uint64, bool)

SubUint64 subtracts two uint64 values and checks for underflow. Returns the result and a boolean indicating if underflow occurred.

func SubUint8

func SubUint8(a, b uint8) (uint8, bool)

SubUint8 subtracts two uint8 values and checks for underflow. Returns the result and a boolean indicating if underflow occurred.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL