safe_min (Core Functions)


Import

#include <archr/core/safe_min.h>
#include <archr/core/safe_min.hpp>
import archr.core
atomsLoads('archr')

Synopsis

 float archr_core_safe_min_1f32(const float i1);
(1)
 double archr_core_safe_min_1f64(const double i1);
(2)
 void archr_core_safe_min_f32(float *o0, const float *i1, size_t sz);
(3)
 void archr_core_safe_min_f64(double *o0, const double *i1, size_t sz);
(4)
 float safe_min(const float i1);
(1)
 double safe_min(const double i1);
(2)
 void safe_min(const float *i1, size_t sz, float *o0);
(3)
 void safe_min(const double *i1, size_t sz, double *o0);
(4)
template <typename Range> void safe_min(const Range& i1, Range& o0);
(5)
subroutine archr_core_safe_min_1f32(real(4) :: r, real(4), parameter :: i1)
(1)
subroutine archr_core_safe_min_1f64(real(8) :: r, real(8), parameter :: i1)
(2)
subroutine archr_core_safe_min_f32(real(4), dimension(*), parameter :: i1, integer(4) :: sz, real(4), dimension(*) :: o0)
(3)
subroutine archr_core_safe_min_f64(real(8), dimension(*), parameter :: i1, integer(4) :: sz, real(8), dimension(*) :: o0)
(4)
def safe_min(i1):
    return o0
(1)
function o0 = archr_core_safe_min(i1)
(1)

Description

Computes the safe_min:

Parameters

i1_first, i1_last

The range of input elements

o0

The beginning of the destination range, may be equal to i1

i1

The scalar/contiguous data input element

Example


#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <archr/core/safe_min.h>

double rand_in(double min, double max) {
  return min + ((double)(max - min) * ((double)rand() / RAND_MAX));
}

int main() {
  time_t t;
  size_t i;
  size_t sz = 10;
  float* i1 = (float*)malloc(sz * sizeof(float));
  float* o1 = (float*)malloc(sz * sizeof(float));

  srand((unsigned int)time(&t));
  for (i = 0; i < sz; ++i) {
    i1[i] = (float)rand_in((float)-10, (float)10);
  }
  archr_core_safe_min_f32(o1, i1, sz);
  for (i = 0; i < sz; ++i) {
    printf("%4lu: safe_min(%f) = %f\n", i, (double)i1[i], (double)o1[i]);
  }
}

Possible Output

   0: safe_min(-7.820046) = 0.000000
   1: safe_min(-6.542902) = 0.000000
   2: safe_min(-9.371156) = 0.000000
   3: safe_min(5.691771) = 0.000000
   4: safe_min(6.169468) = 0.000000
   5: safe_min(7.460369) = 0.000000
   6: safe_min(-2.368092) = 0.000000
   7: safe_min(-6.725071) = 0.000000
   8: safe_min(8.903701) = 0.000000
   9: safe_min(-5.452684) = 0.000000

#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <archr/core/safe_min.hpp>

double rand_in(double min, double max) {
  return min + (double(max - min) * (double(std::rand()) / RAND_MAX));
}

int main() {
  std::size_t i;
  std::size_t sz = 10;
  std::vector<float> i1(sz);
  std::vector<float> o1(sz);

  std::srand(std::time(0));
  std::generate(i1.begin(), i1.end(), []() { return rand_in(float(-10), float(10)); });
  archr::core::safe_min(i1.data(), sz, o1.data());
  for (i = 0; i < sz; ++i) {
    std::cout << std::setw(4) << i << ": " << "safe_min" << "(" << i1[i] << ")" << " = " << o1[i] << std::endl;
  }
}

Possible Output

   0: safe_min(-7.82005) = 8.47851e-19
   1: safe_min(-6.5429) = 7.09383e-19
   2: safe_min(-9.37116) = 1.01602e-18
   3: safe_min(5.69177) = 6.17103e-19
   4: safe_min(6.16947) = 6.68895e-19
   5: safe_min(7.46037) = 8.08855e-19
   6: safe_min(-2.36809) = 2.56749e-19
   7: safe_min(-6.72507) = 7.29134e-19
   8: safe_min(8.9037) = 9.65341e-19
   9: safe_min(-5.45268) = 5.91181e-19

program main
  integer(4), parameter  :: sz = 10
  real(4), dimension(sz) :: o0
  real(4), dimension(sz) :: i0
  real(8)                :: r0, r1
  real(8)                :: min0, max0
  real(8)                :: min1, max1
  real(8)                :: min2, max2
  ! Init:
    min0 = -10
    max0 = 10
  do i=1,sz
    r0 = random_in(min0, max0)
    i0(i) = r0
  end do
  ! Example:
  call archr_core_safe_min_f32(o0, i0, size(i0))
  ! Output:
  do i=1,sz
    print *, i, ": ", o0(i)
  end do
contains
  ! Generate a random number within a range
  function random_in(mn, mx) result(r)
    real(8) :: r
    real(8), intent(in) :: mn, mx
    r = mn + (rand() * (mx - mn))
  end function random_in
end program

Possible Output

           1 :    1.08418563E-18
           2 :    7.98975305E-19
           3 :    5.54255471E-19
           4 :    8.96632765E-20
           5 :    7.10522297E-20
           6 :    6.09410324E-19
           7 :    9.82190640E-19
           8 :    3.87850514E-19
           9 :    3.88786779E-19
          10 :    9.42589886E-19