mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
math: move the math.mathutil
generic min
/max
/abs
fns to math
(#13042)
This commit is contained in:
@@ -1,15 +1,6 @@
|
||||
module math
|
||||
|
||||
// Returns the absolute value.
|
||||
[inline]
|
||||
pub fn abs(x f64) f64 {
|
||||
if x > 0.0 {
|
||||
return x
|
||||
}
|
||||
return -x
|
||||
}
|
||||
|
||||
[inline]
|
||||
[deprecated: 'use math.abs() instead']
|
||||
pub fn fabs(x f64) f64 {
|
||||
if x > 0.0 {
|
||||
return x
|
||||
|
@@ -1,6 +1,6 @@
|
||||
module big
|
||||
|
||||
import math.util
|
||||
import math
|
||||
|
||||
// Compares the magnitude of the two unsigned integers represented the given
|
||||
// digit arrays. Returns -1 if a < b, 0 if a == b and +1 if a > b. Here
|
||||
@@ -40,8 +40,8 @@ fn add_digit_array(operand_a []u32, operand_b []u32, mut sum []u32) {
|
||||
}
|
||||
|
||||
// First pass intersects with both operands
|
||||
smaller_limit := util.imin(operand_a.len, operand_b.len)
|
||||
larger_limit := util.imax(operand_a.len, operand_b.len)
|
||||
smaller_limit := math.min(operand_a.len, operand_b.len)
|
||||
larger_limit := math.max(operand_a.len, operand_b.len)
|
||||
mut a, mut b := if operand_a.len >= operand_b.len {
|
||||
operand_a, operand_b
|
||||
} else {
|
||||
@@ -316,7 +316,7 @@ fn bitwise_or_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
|
||||
}
|
||||
|
||||
fn bitwise_and_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
|
||||
lower := util.imin(operand_a.len, operand_b.len)
|
||||
lower := math.min(operand_a.len, operand_b.len)
|
||||
for index in 0 .. lower {
|
||||
storage[index] = operand_a[index] & operand_b[index]
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
module big
|
||||
|
||||
import math.util
|
||||
import math
|
||||
import math.bits
|
||||
import strings
|
||||
import strconv
|
||||
@@ -37,7 +37,7 @@ pub fn integer_from_int(value int) Integer {
|
||||
return zero_int
|
||||
}
|
||||
return Integer{
|
||||
digits: [u32(util.iabs(value))]
|
||||
digits: [u32(math.abs(value))]
|
||||
signum: int_signum(value)
|
||||
}
|
||||
}
|
||||
@@ -295,7 +295,7 @@ pub fn (integer Integer) - (subtrahend Integer) Integer {
|
||||
fn (integer Integer) add(addend Integer) Integer {
|
||||
a := integer.digits
|
||||
b := addend.digits
|
||||
mut storage := []u32{len: util.imax(a.len, b.len) + 1}
|
||||
mut storage := []u32{len: math.max(a.len, b.len) + 1}
|
||||
add_digit_array(a, b, mut storage)
|
||||
return Integer{
|
||||
...integer
|
||||
@@ -468,7 +468,7 @@ fn check_sign(a Integer) {
|
||||
pub fn (a Integer) bitwise_or(b Integer) Integer {
|
||||
check_sign(a)
|
||||
check_sign(b)
|
||||
mut result := []u32{len: util.imax(a.digits.len, b.digits.len), init: 0}
|
||||
mut result := []u32{len: math.max(a.digits.len, b.digits.len), init: 0}
|
||||
bitwise_or_digit_array(a.digits, b.digits, mut result)
|
||||
return Integer{
|
||||
digits: result
|
||||
@@ -479,7 +479,7 @@ pub fn (a Integer) bitwise_or(b Integer) Integer {
|
||||
pub fn (a Integer) bitwise_and(b Integer) Integer {
|
||||
check_sign(a)
|
||||
check_sign(b)
|
||||
mut result := []u32{len: util.imax(a.digits.len, b.digits.len), init: 0}
|
||||
mut result := []u32{len: math.max(a.digits.len, b.digits.len), init: 0}
|
||||
bitwise_and_digit_array(a.digits, b.digits, mut result)
|
||||
return Integer{
|
||||
digits: result
|
||||
@@ -500,7 +500,7 @@ pub fn (a Integer) bitwise_not() Integer {
|
||||
pub fn (a Integer) bitwise_xor(b Integer) Integer {
|
||||
check_sign(a)
|
||||
check_sign(b)
|
||||
mut result := []u32{len: util.imax(a.digits.len, b.digits.len), init: 0}
|
||||
mut result := []u32{len: math.max(a.digits.len, b.digits.len), init: 0}
|
||||
bitwise_xor_digit_array(a.digits, b.digits, mut result)
|
||||
return Integer{
|
||||
digits: result
|
||||
@@ -817,7 +817,7 @@ pub fn (x Integer) gcd_binary(y Integer) Integer {
|
||||
|
||||
mut az := a.msb()
|
||||
bz := b.msb()
|
||||
shift := util.umin(az, bz)
|
||||
shift := math.min(az, bz)
|
||||
b = b.rshift(bz)
|
||||
|
||||
for a.signum != 0 {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
module big
|
||||
|
||||
import math
|
||||
import math.bits
|
||||
import math.util
|
||||
import strings
|
||||
|
||||
// suppose operand_a bigger than operand_b and both not null.
|
||||
@@ -96,7 +96,7 @@ fn karatsuba_multiply_digit_array(operand_a []u32, operand_b []u32, mut storage
|
||||
}
|
||||
// karatsuba
|
||||
// thanks to the base cases we can pass zero-length arrays to the mult func
|
||||
half := util.imax(operand_a.len, operand_b.len) / 2
|
||||
half := math.max(operand_a.len, operand_b.len) / 2
|
||||
if half <= 0 {
|
||||
panic('Unreachable. Both array have 1 length and multiply_array_by_digit should have been called')
|
||||
}
|
||||
@@ -118,8 +118,8 @@ fn karatsuba_multiply_digit_array(operand_a []u32, operand_b []u32, mut storage
|
||||
mut p_3 := []u32{len: a_l.len + b_l.len + 1, init: 0}
|
||||
multiply_digit_array(a_l, b_l, mut p_3)
|
||||
|
||||
mut tmp_1 := []u32{len: util.imax(a_h.len, a_l.len) + 1, init: 0}
|
||||
mut tmp_2 := []u32{len: util.imax(b_h.len, b_l.len) + 1, init: 0}
|
||||
mut tmp_1 := []u32{len: math.max(a_h.len, a_l.len) + 1, init: 0}
|
||||
mut tmp_2 := []u32{len: math.max(b_h.len, b_l.len) + 1, init: 0}
|
||||
add_digit_array(a_h, a_l, mut tmp_1)
|
||||
add_digit_array(b_h, b_l, mut tmp_2)
|
||||
|
||||
@@ -170,8 +170,8 @@ fn lshift_byte_in_place(mut a []u32, byte_nb int) {
|
||||
fn add_in_place(mut a []u32, b []u32) {
|
||||
len_a := a.len
|
||||
len_b := b.len
|
||||
max := util.imax(len_a, len_b)
|
||||
min := util.imin(len_a, len_b)
|
||||
max := math.max(len_a, len_b)
|
||||
min := math.min(len_a, len_b)
|
||||
mut carry := u64(0)
|
||||
for index in 0 .. min {
|
||||
partial := carry + a[index] + b[index]
|
||||
@@ -197,8 +197,8 @@ fn add_in_place(mut a []u32, b []u32) {
|
||||
fn subtract_in_place(mut a []u32, b []u32) {
|
||||
len_a := a.len
|
||||
len_b := b.len
|
||||
max := util.imax(len_a, len_b)
|
||||
min := util.imin(len_a, len_b)
|
||||
max := math.max(len_a, len_b)
|
||||
min := math.min(len_a, len_b)
|
||||
mut carry := u32(0)
|
||||
mut new_carry := u32(0)
|
||||
for index in 0 .. min {
|
||||
|
@@ -61,24 +61,6 @@ pub fn digits(_n int, base int) []int {
|
||||
return res
|
||||
}
|
||||
|
||||
// max returns the maximum value of the two provided.
|
||||
[inline]
|
||||
pub fn max(a f64, b f64) f64 {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// min returns the minimum value of the two provided.
|
||||
[inline]
|
||||
pub fn min(a f64, b f64) f64 {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// minmax returns the minimum and maximum value of the two provided.
|
||||
pub fn minmax(a f64, b f64) (f64, f64) {
|
||||
if a < b {
|
||||
|
22
vlib/math/mathutil.v
Normal file
22
vlib/math/mathutil.v
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2019-2022 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
|
||||
|
||||
// min returns the minimum of `a` and `b`
|
||||
[inline]
|
||||
pub fn min<T>(a T, b T) T {
|
||||
return if a < b { a } else { b }
|
||||
}
|
||||
|
||||
// max returns the maximum of `a` and `b`
|
||||
[inline]
|
||||
pub fn max<T>(a T, b T) T {
|
||||
return if a > b { a } else { b }
|
||||
}
|
||||
|
||||
// abs returns the absolute value of `a`
|
||||
[inline]
|
||||
pub fn abs<T>(a T) T {
|
||||
return if a > 0 { a } else { -a }
|
||||
}
|
@@ -3,16 +3,22 @@
|
||||
// that can be found in the LICENSE file.
|
||||
module mathutil
|
||||
|
||||
[deprecated: 'use math.min instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn min<T>(a T, b T) T {
|
||||
return if a < b { a } else { b }
|
||||
}
|
||||
|
||||
[deprecated: 'use math.max instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn max<T>(a T, b T) T {
|
||||
return if a > b { a } else { b }
|
||||
}
|
||||
|
||||
[deprecated: 'use math.abs instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn abs<T>(a T) T {
|
||||
return if a > 0 { a } else { -a }
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import math.mathutil as mu
|
||||
import math as mu
|
||||
|
||||
fn test_min() {
|
||||
assert mu.min(42, 13) == 13
|
||||
|
22
vlib/math/mathutil_test.v
Normal file
22
vlib/math/mathutil_test.v
Normal file
@@ -0,0 +1,22 @@
|
||||
import math
|
||||
|
||||
fn test_min() {
|
||||
assert math.min(42, 13) == 13
|
||||
assert math.min(5, -10) == -10
|
||||
assert math.min(7.1, 7.3) == 7.1
|
||||
assert math.min(u32(32), u32(17)) == 17
|
||||
}
|
||||
|
||||
fn test_max() {
|
||||
assert math.max(42, 13) == 42
|
||||
assert math.max(5, -10) == 5
|
||||
assert math.max(7.1, 7.3) == 7.3
|
||||
assert math.max(u32(60), u32(17)) == 60
|
||||
}
|
||||
|
||||
fn test_abs() {
|
||||
assert math.abs(99) == 99
|
||||
assert math.abs(-10) == 10
|
||||
assert math.abs(1.2345) == 1.2345
|
||||
assert math.abs(-5.5) == 5.5
|
||||
}
|
@@ -4,36 +4,48 @@
|
||||
module util
|
||||
|
||||
// imin returns the smallest of two integer values
|
||||
[deprecated: 'use math.min instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn imin(a int, b int) int {
|
||||
return if a < b { a } else { b }
|
||||
}
|
||||
|
||||
// imin returns the biggest of two integer values
|
||||
[deprecated: 'use math.max instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn imax(a int, b int) int {
|
||||
return if a > b { a } else { b }
|
||||
}
|
||||
|
||||
// iabs returns an integer as absolute value
|
||||
[deprecated: 'use math.abs instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn iabs(v int) int {
|
||||
return if v > 0 { v } else { -v }
|
||||
}
|
||||
|
||||
// umin returns the smallest of two u32 values
|
||||
[deprecated: 'use math.min instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn umin(a u32, b u32) u32 {
|
||||
return if a < b { a } else { b }
|
||||
}
|
||||
|
||||
// umax returns the biggest of two u32 values
|
||||
[deprecated: 'use math.max instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn umax(a u32, b u32) u32 {
|
||||
return if a > b { a } else { b }
|
||||
}
|
||||
|
||||
// uabs returns an u32 as absolute value
|
||||
[deprecated: 'use math.abs instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn uabs(v u32) u32 {
|
||||
return if v > 0 { v } else { -v }
|
||||
@@ -41,6 +53,8 @@ pub fn uabs(v u32) u32 {
|
||||
|
||||
// fmin_32 returns the smallest `f32` of input `a` and `b`.
|
||||
// Example: assert fmin_32(2.0,3.0) == 2.0
|
||||
[deprecated: 'use math.min instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn fmin_32(a f32, b f32) f32 {
|
||||
return if a < b { a } else { b }
|
||||
@@ -48,6 +62,8 @@ pub fn fmin_32(a f32, b f32) f32 {
|
||||
|
||||
// fmax_32 returns the largest `f32` of input `a` and `b`.
|
||||
// Example: assert fmax_32(2.0,3.0) == 3.0
|
||||
[deprecated: 'use math.max instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn fmax_32(a f32, b f32) f32 {
|
||||
return if a > b { a } else { b }
|
||||
@@ -55,6 +71,8 @@ pub fn fmax_32(a f32, b f32) f32 {
|
||||
|
||||
// fabs_32 returns the absolute value of `a` as a `f32` value.
|
||||
// Example: assert fabs_32(-2.0) == 2.0
|
||||
[deprecated: 'use math.abs instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn fabs_32(v f32) f32 {
|
||||
return if v > 0 { v } else { -v }
|
||||
@@ -62,6 +80,8 @@ pub fn fabs_32(v f32) f32 {
|
||||
|
||||
// fmin_64 returns the smallest `f64` of input `a` and `b`.
|
||||
// Example: assert fmin_64(2.0,3.0) == 2.0
|
||||
[deprecated: 'use math.min instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn fmin_64(a f64, b f64) f64 {
|
||||
return if a < b { a } else { b }
|
||||
@@ -69,6 +89,8 @@ pub fn fmin_64(a f64, b f64) f64 {
|
||||
|
||||
// fmax_64 returns the largest `f64` of input `a` and `b`.
|
||||
// Example: assert fmax_64(2.0,3.0) == 3.0
|
||||
[deprecated: 'use math.max instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn fmax_64(a f64, b f64) f64 {
|
||||
return if a > b { a } else { b }
|
||||
@@ -76,6 +98,8 @@ pub fn fmax_64(a f64, b f64) f64 {
|
||||
|
||||
// fabs_64 returns the absolute value of `a` as a `f64` value.
|
||||
// Example: assert fabs_64(-2.0) == f64(2.0)
|
||||
[deprecated: 'use math.abs instead']
|
||||
[deprecated_after: '2022-01-19']
|
||||
[inline]
|
||||
pub fn fabs_64(v f64) f64 {
|
||||
return if v > 0 { v } else { -v }
|
||||
|
Reference in New Issue
Block a user