i16math

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2020 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package i16math provides some utilities for the int16 type that mirror those found in the built-in math package.

Index

Examples

Constants

View Source
const (
	MaxValue = math.MaxInt16
	MinValue = math.MinInt16
)

int16 limit values.

Variables

This section is empty.

Functions

func Abs

func Abs(x int16) int16

Abs returns the absolute value of x, or |x|. If |x| exceeds MaxValue, MaxValue is returned.

Example
fmt.Println(Abs(0))
fmt.Println(Abs(1))
fmt.Println(Abs(-1))
fmt.Println(Abs(MaxValue) == MaxValue)
fmt.Println(Abs(MinValue) == MaxValue)
Output:

0
1
1
true
true

func CeilToMultiple

func CeilToMultiple(x, y int16) int16

CeilToMultiple returns the least multiple of y that is at least x. If x+y exceeds MaxValue, then the result is undefined.

Special cases:

CeilToMultiple(x, 0) = x
CeilToMultiple(x, -1) = x
Example
fmt.Println(CeilToMultiple(6, 3))
fmt.Println(CeilToMultiple(7, 3))
fmt.Println(CeilToMultiple(8, 3))
fmt.Println(CeilToMultiple(9, 3))

fmt.Println(CeilToMultiple(-6, 3))
fmt.Println(CeilToMultiple(-7, 3))
fmt.Println(CeilToMultiple(-8, 3))
fmt.Println(CeilToMultiple(-9, 3))
Output:

6
9
9
9
-6
-6
-6
-9

func Dim

func Dim(x, y int16) uint16

Dim returns the maximum of x-y or 0.

func FloorToMultiple

func FloorToMultiple(x, y int16) int16

FloorToMultiple returns the greatest multiple of y that is at most x. If x-y is less than MinValue, then the result is undefined.

Special cases:

FloorToMultiple(x, 0) = x
FloorToMultiple(x, -1) = x
Example
fmt.Println(FloorToMultiple(6, 3))
fmt.Println(FloorToMultiple(7, 3))
fmt.Println(FloorToMultiple(8, 3))
fmt.Println(FloorToMultiple(9, 3))

fmt.Println(FloorToMultiple(-6, 3))
fmt.Println(FloorToMultiple(-7, 3))
fmt.Println(FloorToMultiple(-8, 3))
fmt.Println(FloorToMultiple(-9, 3))
Output:

6
6
6
9
-6
-9
-9
-9

func Max

func Max(x, y int16) int16

Max returns the larger of x or y.

Example
fmt.Println(Max(0, 0))
fmt.Println(Max(0, 1))
fmt.Println(Max(1, 0))
fmt.Println(Max(MaxValue, 0) == MaxValue)
fmt.Println(Max(MinValue, MaxValue) == MaxValue)
Output:

0
1
1
true
true

func Min

func Min(x, y int16) int16

Min returns the smaller of x or y.

Example
fmt.Println(Min(1, 1))
fmt.Println(Min(0, 1))
fmt.Println(Min(1, 0))
fmt.Println(Min(MinValue, 0) == MinValue)
fmt.Println(Min(MinValue, MaxValue) == MinValue)
Output:

1
0
0
true
true

func RoundToMultiple

func RoundToMultiple(x, y int16) int16

RoundToMultiple returns the multiple of y closest to x, rounding ties away from zero. If x + y/2 exceeds MaxValue, or if x - y/2 is less than MinValue, then the result is undefined.

Special cases:

RoundToMultiple(x, 0) = x
RoundToMultiple(x, -1) = x
Example (Even)
fmt.Println(RoundToMultiple(4, 2))
fmt.Println(RoundToMultiple(5, 2))
fmt.Println(RoundToMultiple(6, 2))

fmt.Println(RoundToMultiple(-4, 2))
fmt.Println(RoundToMultiple(-5, 2))
fmt.Println(RoundToMultiple(-6, 2))
Output:

4
6
6
-4
-6
-6
Example (Odd)
fmt.Println(RoundToMultiple(6, 3))
fmt.Println(RoundToMultiple(7, 3))
fmt.Println(RoundToMultiple(8, 3))
fmt.Println(RoundToMultiple(9, 3))

fmt.Println(RoundToMultiple(-6, 3))
fmt.Println(RoundToMultiple(-7, 3))
fmt.Println(RoundToMultiple(-8, 3))
fmt.Println(RoundToMultiple(-9, 3))
Output:

6
6
9
9
-6
-6
-9
-9

Types

This section is empty.

Jump to

Keyboard shortcuts

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