|
| 1 | +--- |
| 2 | +title: math |
| 3 | +--- |
| 4 | + |
| 5 | +# The `stdlib_math` module |
| 6 | + |
| 7 | +[TOC] |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +`stdlib_math` module provides with general purpose mathematical functions. |
| 12 | + |
| 13 | + |
| 14 | +## Procedures and Methods provided |
| 15 | + |
| 16 | + |
| 17 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 18 | +### `clip` function |
| 19 | + |
| 20 | +#### Description |
| 21 | + |
| 22 | +Limits the input value `x` to the given interval [`xmin`, `xmax`] (interval is `xmin` and `xmax` inclusive). |
| 23 | + |
| 24 | +Returns a value which lies in the given interval and is closest to the input value `x`. |
| 25 | + |
| 26 | +If the input value `x` already lies in the given interval, then the output value will be equal to the input value. |
| 27 | + |
| 28 | +Note: A valid input must **NOT** have `xmin` value greater than `xmax` value. |
| 29 | + |
| 30 | +#### Syntax |
| 31 | + |
| 32 | +`res = [[stdlib_math(module):clip(interface)]] (x, xmin, xmax)` |
| 33 | + |
| 34 | +#### Status |
| 35 | + |
| 36 | +Experimental |
| 37 | + |
| 38 | +#### Class |
| 39 | + |
| 40 | +Elemental function. |
| 41 | + |
| 42 | +#### Argument(s) |
| 43 | + |
| 44 | +`x`: scalar of either `integer` or `real`. This argument is `intent(in)`. |
| 45 | +`xmin`: scalar of either `integer` or `real`. This argument is `intent(in)`. |
| 46 | +`xmax`: scalar of either `integer` or `real`. This argument is `intent(in)`. |
| 47 | +All arguments must have same `type` and same `kind`. |
| 48 | + |
| 49 | +#### Output value or Result value |
| 50 | + |
| 51 | +Output is a scalar of either `integer` or `real` depending on the arguments. The output value will have `type` and `kind` same as to that of the arguments. |
| 52 | + |
| 53 | +#### Example(s) of usage |
| 54 | + |
| 55 | +##### Example 1: |
| 56 | + |
| 57 | +Here inputs are of type `integer` and kind `int32` |
| 58 | +```fortran |
| 59 | +program demo |
| 60 | + use stdlib_math |
| 61 | + use iso_fortran_env |
| 62 | + implicit none |
| 63 | + integer(int32) :: x |
| 64 | + integer(int32) :: xmin |
| 65 | + integer(int32) :: xmax |
| 66 | + integer(int32) :: clipped_value |
| 67 | +
|
| 68 | + xmin = -5 |
| 69 | + ! xmin <- -5 |
| 70 | + xmax = 5 |
| 71 | + ! xmax <- 5 |
| 72 | + x = 12 |
| 73 | + ! x <- 12 |
| 74 | +
|
| 75 | + clipped_value = clip(x, xmin, xmax) |
| 76 | + ! clipped_value <- 5 |
| 77 | +end program demo |
| 78 | +``` |
| 79 | + |
| 80 | +##### Example 2: |
| 81 | + |
| 82 | +Here inputs are of type `real` and kind `real32` (or `sp`) |
| 83 | +```fortran |
| 84 | +program demo |
| 85 | + use stdlib_math |
| 86 | + use iso_fortran_env |
| 87 | + implicit none |
| 88 | + real(real32) :: x |
| 89 | + real(real32) :: xmin |
| 90 | + real(real32) :: xmax |
| 91 | + real(real32) :: clipped_value |
| 92 | +
|
| 93 | + xmin = -5.76999998 |
| 94 | + ! xmin <- -5.76999998 |
| 95 | + xmax = 3.02500010 |
| 96 | + ! xmax <- 3.02500010 |
| 97 | + x = 3.02500010 |
| 98 | + ! x <- 3.02500010 |
| 99 | +
|
| 100 | + clipped_value = clip(x, xmin, xmax) |
| 101 | + ! clipped_value <- 3.02500010 |
| 102 | +end program demo |
| 103 | +``` |
0 commit comments