maths

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2024 License: Apache-2.0 Imports: 3 Imported by: 12

README

godoc codecov Go Report Card

maths - mathematics and number-type utilities

maths is a package for general purpose mathematics and other number-type things.

Installation

> go get github.com/go-corelibs/maths@latest

Examples

Clamp, ToInt, IntegerLen

func main() {
    clamped := maths.Clamp(2.5, 0.0, 1.0)
    // clamped == float64(1.0)
    integer := maths.ToInt(10.0)
    // integer == int(10)
    count := maths.IntegerLen(1010)
    // count == 4
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2023 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsFloat32 added in v1.1.0

func AsFloat32[V Number](v V) float32

func AsFloat64 added in v1.1.0

func AsFloat64[V Number](v V) float64

func AsInt

func AsInt[V Number](v V) int

func AsInt64

func AsInt64[V Number](v V) int64

func AsUint

func AsUint[V Number](v V) uint

func AsUint64

func AsUint64[V Number](v V) uint64

func Atoi

func Atoi(v interface{}, def ...int) (number int)

Atoi is a wrapper around strconv.Atoi with the given value converted to a string first using fmt.Sprintf with a "%v" replacement

def is an optional default value if the strconv.Atoi call returns an error, only the first def value is ever used and if there are no def values provided, math.MaxInt is returned

func Ceil

func Ceil[V Number](value, max V) V

Ceil returns the value if it is less-than-or-equal-to the maximum and returns the maximum otherwise

func Clamp

func Clamp[V Number](value, min, max V) V

Clamp returns the value if it is greater-than-or-equal-to the minimum and less-than-or-equal-to the maximum arguments; if the value is less than the minimum, the minimum is returned; if the value is greater than the maximum, the maximum is returned

func DecimalLen added in v1.1.0

func DecimalLen[V Decimal](v V) (length int)

DecimalLen returns the number of digits in the generic Decimal given

func Floor

func Floor[V Number](value, min V) V

Floor returns the value if it is greater-than-or-equal-to the minimum and returns the minimum otherwise

func IntegerLen added in v1.1.0

func IntegerLen[V Integers](v V) (length int)

IntegerLen returns the number of digits in the generic Integers given

func Round

func Round[V Decimal](x V) (rounded int)

Round returns the value rounded to the nearest whole value

func RoundDown

func RoundDown[V Decimal](value V) (rounded int)

RoundDown returns the value rounded down

func RoundUp

func RoundUp[V Decimal](value V) (rounded int)

RoundUp returns the value rounded up

func ToFloat64

func ToFloat64(v interface{}, def ...float64) float64

func ToInt

func ToInt(v interface{}, def ...int) int

func ToInt64

func ToInt64(v interface{}, def ...int64) int64

func ToUint

func ToUint(v interface{}, def ...uint) uint

func ToUint64

func ToUint64(v interface{}, def ...uint64) uint64

Types

type Complex

type Complex interface {
	~complex64 | ~complex128
}

type Decimal

type Decimal interface {
	~float32 | ~float64
}

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Integers

type Integers interface {
	Integer | UnsignedInteger
}

type Number

type Number interface {
	UnsignedInteger | Integer | Decimal
}

type UnsignedInteger

type UnsignedInteger interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Jump to

Keyboard shortcuts

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