mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
69 lines
1.5 KiB
V
69 lines
1.5 KiB
V
|
// 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.')
|
||
|
}
|
||
|
}
|