Debian Codegraph @

libphobos/libdruntime/core/internal/util/math.d

raw
1 // Written in the D programming language
2
3 /**
4 * Internal math utilities.
5 *
6 * Copyright: The D Language Foundation 2021.
7 * License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
8 * Authors: Luís Ferreira
9 * Source: $(DRUNTIMESRC core/internal/util/_math.d)
10 */
11 module core.internal.util.math;
12
13 /**
14 * Calculates the maximum of the passed arguments
15 * Params:
16 * a = first value to select the maximum from
17 * b = second value to select the maximum from
18 * Returns: The maximum of the passed-in values.
19 */
20 T max(T)(T a, T b) pure nothrow @nogc @safe
21 {
22 return b > a ? b : a;
23 }
24
25 /**
26 * Calculates the minimum of the passed arguments
27 * Params:
28 * a = first value to select the minimum from
29 * b = second value to select the minimum from
30 * Returns: The minimum of the passed-in values.
31 */
32 T min(T)(T a, T b) pure nothrow @nogc @safe
33 {
34 return b < a ? b : a;
35 }
36
37 ///
38 @safe pure @nogc nothrow
39 unittest
40 {
41 assert(max(1,3) == 3);
42 assert(max(3,1) == 3);
43 assert(max(1,1) == 1);
44 }
45
46 ///
47 @safe pure @nogc nothrow
48 unittest
49 {
50 assert(min(1,3) == 1);
51 assert(min(3,1) == 1);
52 assert(min(1,1) == 1);
53 }