1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

math: add a pure V math.mathutil, with generic min, max and abs functions (#9176), and use it consistently

This commit is contained in:
Lukas Neubert
2021-03-12 10:28:04 +01:00
committed by GitHub
parent 530b981765
commit a67d49050c
15 changed files with 193 additions and 125 deletions

View File

@@ -0,0 +1,31 @@
// Copyright (c) 2019-2021 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 mathutil
[inline]
pub fn min<T>(a T, b T) T {
if a < b {
return a
} else {
return b
}
}
[inline]
pub fn max<T>(a T, b T) T {
if a > b {
return a
} else {
return b
}
}
[inline]
pub fn abs<T>(a T) T {
if a > 0 {
return a
} else {
return -a
}
}

View File

@@ -0,0 +1,22 @@
import math.mathutil as mu
fn test_min() {
assert mu.min(42, 13) == 13
assert mu.min(5, -10) == -10
assert mu.min(7.1, 7.3) == 7.1
assert mu.min(u32(32), u32(17)) == 17
}
fn test_max() {
assert mu.max(42, 13) == 42
assert mu.max(5, -10) == 5
assert mu.max(7.1, 7.3) == 7.3
assert mu.max(u32(60), u32(17)) == 60
}
fn test_abs() {
assert mu.abs(99) == 99
assert mu.abs(-10) == 10
assert mu.abs(1.2345) == 1.2345
assert mu.abs(-5.5) == 5.5
}

82
vlib/math/util/util.v Normal file
View File

@@ -0,0 +1,82 @@
// Copyright (c) 2019-2021 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 util
// imin returns the smallest of two integer values
[inline]
pub fn imin(a int, b int) int {
return if a < b { a } else { b }
}
// imin returns the biggest of two integer values
[inline]
pub fn imax(a int, b int) int {
return if a > b { a } else { b }
}
// iabs returns an integer as absolute value
[inline]
pub fn iabs(v int) int {
return if v > 0 { v } else { -v }
}
// umin returns the smallest of two u32 values
[inline]
pub fn umin(a u32, b u32) u32 {
return if a < b { a } else { b }
}
// umax returns the biggest of two u32 values
[inline]
pub fn umax(a u32, b u32) u32 {
return if a > b { a } else { b }
}
// uabs returns an u32 as absolute value
[inline]
pub fn uabs(v u32) u32 {
return if v > 0 { v } else { -v }
}
// fmin_32 returns the smallest `f32` of input `a` and `b`.
// Example: assert fmin_32(2.0,3.0) == 2.0
[inline]
pub fn fmin_32(a f32, b f32) f32 {
return if a < b { a } else { b }
}
// fmax_32 returns the largest `f32` of input `a` and `b`.
// Example: assert fmax_32(2.0,3.0) == 3.0
[inline]
pub fn fmax_32(a f32, b f32) f32 {
return if a > b { a } else { b }
}
// fabs_32 returns the absolute value of `a` as a `f32` value.
// Example: assert fabs_32(-2.0) == 2.0
[inline]
pub fn fabs_32(v f32) f32 {
return if v > 0 { v } else { -v }
}
// fmin_64 returns the smallest `f64` of input `a` and `b`.
// Example: assert fmin_64(2.0,3.0) == 2.0
[inline]
pub fn fmin_64(a f64, b f64) f64 {
return if a < b { a } else { b }
}
// fmax_64 returns the largest `f64` of input `a` and `b`.
// Example: assert fmax_64(2.0,3.0) == 3.0
[inline]
pub fn fmax_64(a f64, b f64) f64 {
return if a > b { a } else { b }
}
// fabs_64 returns the absolute value of `a` as a `f64` value.
// Example: assert fabs_64(-2.0) == f64(2.0)
[inline]
pub fn fabs_64(v f64) f64 {
return if v > 0 { v } else { -v }
}