numbers

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package numbers provides numeric types, utilities, and range abstractions for rounding, scaling, and yield adjustment calculations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RoundToDecimalPlaces

func RoundToDecimalPlaces(value float32, precision uint8) float32

RoundToDecimalPlaces rounds a float32 value to the specified number of decimal places.

Example
package main

import (
	"fmt"

	"github.com/primandproper/primitives-go/numbers"
)

func main() {
	result := numbers.RoundToDecimalPlaces(3.14159, 2)
	fmt.Println(result)
}
Output:
3.14

func Scale

func Scale(value, factor float32, precision ...uint8) float32

Scale multiplies a value by a scaling factor and rounds to the specified precision (default: 2). Useful for scaling quantities by a factor. For example, Scale(2.5, 2.0) would return 5.0 (doubling the quantity).

Example
package main

import (
	"fmt"

	"github.com/primandproper/primitives-go/numbers"
)

func main() {
	// Double a quantity of 2.5
	result := numbers.Scale(2.5, 2.0)
	fmt.Println(result)
}
Output:
5

func ScaleToYield

func ScaleToYield(originalValue float32, originalYield, desiredYield int, precision ...uint8) float32

ScaleToYield scales a quantity from an original yield to a desired yield. The optional precision parameter specifies the number of decimal places to round to (default: 2). For example, ScaleToYield(2.0, 4, 6) returns 3.0 (scaling from 4 units to 6).

Example
package main

import (
	"fmt"

	"github.com/primandproper/primitives-go/numbers"
)

func main() {
	// Scale from 4 servings to 6 servings
	result := numbers.ScaleToYield(2.0, 4, 6)
	fmt.Println(result)
}
Output:
3

Types

type MinRange

type MinRange[T Numeric] struct {
	Min T  `json:"min"`
	Max *T `json:"max,omitempty"`
}

MinRange represents a range with a required minimum and optional maximum.

func (*MinRange[T]) ValidateWithContext

func (x *MinRange[T]) ValidateWithContext(ctx context.Context) error

type Numeric

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

Numeric is a constraint for all built-in numeric types.

type OpenRange

type OpenRange[T Numeric] struct {
	Min *T `json:"min,omitempty"`
	Max *T `json:"max,omitempty"`
}

OpenRange represents a range where both minimum and maximum are optional.

func (*OpenRange[T]) ValidateWithContext

func (x *OpenRange[T]) ValidateWithContext(ctx context.Context) error

ValidateWithContext enforces the one thing an OpenRange can get wrong.

Both bounds are optional, so neither is Required and an entirely empty range — "no bound either way" — is valid. What is not valid is the same inversion MinRange refuses: a maximum below the minimum describes a range no value can satisfy, and a filter built from one silently returns nothing rather than reporting that it was asked for the impossible. Absence of either bound has nothing to compare, and passes.

Jump to

Keyboard shortcuts

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