mathutil

package
v13.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 1 Imported by: 16

Documentation

Overview

Package mathutil provides some additional math methods

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[N NumericNeg](val N) N

Abs returns absolute value

Example
fmt.Println(Abs(10))
fmt.Println(Abs(-10))
Output:

10
10

func B added in v13.10.1

func B[N Numeric](cond bool, positive, negative N) N

B is shorthand for choosing value by condition

Example
isRoot := true

fmt.Println(B(isRoot, 0, 1000))
fmt.Println(B(!isRoot, 0, 1000))
Output:

0
1000

func Between

func Between[N Numeric](val, min, max N) N

Between returns value between min and max values

Example
fmt.Println(Between(10, 1, 5))
fmt.Println(Between(-3, 1, 5))
fmt.Println(Between(4, 1, 5))
Output:

5
1
4

func FromPerc added in v13.5.1

func FromPerc(perc float64, total float64) float64

FromPerc calculates value from percentage

Example
fmt.Printf("%g\n", FromPerc(15.0, 2860))
Output:

429

func IsFloat added in v13.5.0

func IsFloat(s string) bool

IsFloat returns true if given string contains float symbols.

Note that this method does not validate the given value.

Example
fmt.Println(IsFloat("test"))
fmt.Println(IsFloat("74.6131"))
fmt.Println(IsFloat("-10.4"))
Output:

false
true
true

func IsInt added in v13.5.0

func IsInt(s string) bool

IsInt returns true if given string contains int symbols.

Note that this method does not validate the given value.

Example
fmt.Println(IsInt("test"))
fmt.Println(IsInt("746131"))
fmt.Println(IsInt("-194"))
Output:

false
true
true

func IsNumber added in v13.5.0

func IsNumber(s string) bool

IsNumber returns true if given string contains number symbols (int or float).

Note that this method does not validate the given value.

Example
fmt.Println(IsNumber("test"))
fmt.Println(IsNumber("746131"))
fmt.Println(IsNumber("-10.431"))
Output:

false
true
true

func Max

func Max[N Numeric](val1, val2 N) N

Max returns a greater value

Example
fmt.Println(Max(1, 10))
fmt.Println(Max(3, -3))
Output:

10
3

func Min

func Min[N Numeric](val1, val2 N) N

Min returns a smaller value

Example
fmt.Println(Min(1, 10))
fmt.Println(Min(3, -3))
Output:

1
-3

func Perc

func Perc[N Numeric](current, total N) float64

Perc calculates percentage

Example
fmt.Printf("%g%%\n", Perc(180, 600))
Output:

30%

func Round

func Round(v float64, p int) float64

Round returns rounded value

Example
fmt.Println(Round(3.14159, 2))
Output:

3.14

Types

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type Numeric

type Numeric interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64
}

type NumericNeg

type NumericNeg interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
}

Jump to

Keyboard shortcuts

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