# mathx

package
Version: v0.0.0-...-cd40a3f Latest Latest

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

Go to latest
Published: Jul 26, 2021 License: Apache-2.0

### mathx

Package `mathx` contains math utilities. It means to be a complement to the standard `math` package.

## Documentation ¶

### Overview ¶

Package mathx contains math utilities. It means to be a complement to the standard math package.

### Constants ¶

View Source
```const (
MaxUint = ^uint(0)
MinUint = 0
MaxInt  = int(MaxUint >> 1)
MinInt  = -MaxInt - 1
)```

Compile time constants to determine the min and max values of the int and uint types.

### Variables ¶

This section is empty.

### Functions ¶

#### func AbsInt ¶

`func AbsInt(i int) int`

AbsInt returns the absolute value of i.

#### func Near ¶

`func Near(a, b, eps float64) bool`

Near reports if 2 float64 numbers are "near" to each other. The caller is responsible to provide a sensible epsilon.

"near" is defined as the following:

```near := math.Abs(a - b) < eps
```

Corner cases:

```1. if a==b, result is true (eps will not be checked, may be NaN)
2. Inf is near to Inf (even if eps=NaN; consequence of 1.)
3. -Inf is near to -Inf (even if eps=NaN; consequence of 1.)
4. NaN is not near to anything (not even to NaN)
5. eps=Inf results in true (unless any of a or b is NaN)
```

#### func Round ¶

`func Round(x, unit float64) float64`

Round returns x rounded to the given unit. Tip: x is "arbitrary", maybe greater than 1. For example:

```Round(0.363636, 0.001) // 0.364
Round(0.363636, 0.01)  // 0.36
Round(0.363636, 0.1)   // 0.4
Round(0.363636, 0.05)  // 0.35
Round(3.2, 1)          // 3
Round(32, 5)           // 30
Round(33, 5)           // 35
Round(32, 10)          // 30
```

For details, see https://stackoverflow.com/a/39544897/1705598

Example
```fmt.Printf("%.4f\n", Round(0.363636, 0.001)) // 0.364
fmt.Printf("%.4f\n", Round(0.363636, 0.01))  // 0.36
fmt.Printf("%.4f\n", Round(0.363636, 0.1))   // 0.4
fmt.Printf("%.4f\n", Round(0.363636, 0.05))  // 0.35
fmt.Printf("%.4f\n", Round(3.2, 1))          // 3
fmt.Printf("%.4f\n", Round(32, 5))           // 30
fmt.Printf("%.4f\n", Round(33, 5))           // 35
fmt.Printf("%.4f\n", Round(32, 10))          // 30

fmt.Printf("%.4f\n", Round(-0.363636, 0.001)) // -0.364
fmt.Printf("%.4f\n", Round(-0.363636, 0.01))  // -0.36
fmt.Printf("%.4f\n", Round(-0.363636, 0.1))   // -0.4
fmt.Printf("%.4f\n", Round(-0.363636, 0.05))  // -0.35
fmt.Printf("%.4f\n", Round(-3.2, 1))          // -3
fmt.Printf("%.4f\n", Round(-32, 5))           // -30
fmt.Printf("%.4f\n", Round(-33, 5))           // -35
fmt.Printf("%.4f\n", Round(-32, 10))          // -30
```
```Output:

0.3640
0.3600
0.4000
0.3500
3.0000
30.0000
35.0000
30.0000
-0.3640
-0.3600
-0.4000
-0.3500
-3.0000
-30.0000
-35.0000
-30.0000
```

### Types ¶

This section is empty.

## Directories ¶

Path Synopsis
Package randx contains random-related utilities.
Package randx contains random-related utilities.