mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
math: add maxof[T]()
and minof[T]()
(#17905)
This commit is contained in:
68
vlib/math/limit.v
Normal file
68
vlib/math/limit.v
Normal file
@ -0,0 +1,68 @@
|
||||
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
module math
|
||||
|
||||
// maxof returns the maximum value of the type `T`
|
||||
[inline]
|
||||
pub fn maxof[T]() T {
|
||||
$if T is i8 {
|
||||
return max_i8
|
||||
} $else $if T is i16 {
|
||||
return max_i16
|
||||
} $else $if T is int {
|
||||
return max_i32
|
||||
} $else $if T is i32 {
|
||||
return max_i32
|
||||
} $else $if T is i64 {
|
||||
return max_i64
|
||||
} $else $if T is u8 {
|
||||
return max_u8
|
||||
} $else $if T is byte {
|
||||
return max_u8
|
||||
} $else $if T is u16 {
|
||||
return max_u16
|
||||
} $else $if T is u32 {
|
||||
return max_u32
|
||||
} $else $if T is u64 {
|
||||
return max_u64
|
||||
} $else $if T is f32 {
|
||||
return max_f32
|
||||
} $else $if T is f64 {
|
||||
return max_f64
|
||||
} $else {
|
||||
panic('A maximum value of the type `${typeof[T]().name}` is not defined.')
|
||||
}
|
||||
}
|
||||
|
||||
// minof returns the minimum value of the type `T`
|
||||
[inline]
|
||||
pub fn minof[T]() T {
|
||||
$if T is i8 {
|
||||
return min_i8
|
||||
} $else $if T is i16 {
|
||||
return min_i16
|
||||
} $else $if T is int {
|
||||
return min_i32
|
||||
} $else $if T is i32 {
|
||||
return min_i32
|
||||
} $else $if T is i64 {
|
||||
return min_i64
|
||||
} $else $if T is u8 {
|
||||
return min_u8
|
||||
} $else $if T is byte {
|
||||
return min_u8
|
||||
} $else $if T is u16 {
|
||||
return min_u16
|
||||
} $else $if T is u32 {
|
||||
return min_u32
|
||||
} $else $if T is u64 {
|
||||
return min_u64
|
||||
} $else $if T is f32 {
|
||||
return -max_f32
|
||||
} $else $if T is f64 {
|
||||
return -max_f64
|
||||
} $else {
|
||||
panic('A minimum value of the type `${typeof[T]().name}` is not defined.')
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user