Skip to content

proposal: math: MinOf and MaxOf for generic numeric limits #50019

Open
@rogpeppe

Description

@rogpeppe

Currently when writing a function involving a generic numeric type, there's no easy way to determine the maximum or minimum possible value of that type, because the math.Min* and math.Max* constants are all type-specific.

I propose that we add the following functions to the math package:

// MinOf returns the minimum possible value of type T.
func MinOf[T contraints.Integer | constraints.Float]() T

// MaxOf returns the maximum possible value of type T.
func MaxOf[T contraints.Integer | constraints.Float]() T

It's currently not possible to write the above functions without using reflection until #45380
is fixed, but here's an approximation:

https://gotipplay.golang.org/p/0i3Wc7i4dJs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions