float

Overview

Module with functions and types to work with float values.

Types and Definitions

cFloatInfo

struct cFloatInfo
{
   uint8_t sign;
   uint8_t exponent;
   uint32_t mantissa;
};
typedef struct cFloatInfo cFloatInfo;

cFloatInfo represents the three sections of a float value.

Generated

cFloatSlice

struct cFloatSlice
{
   int64_t s;
   float const* v;
};
typedef struct cFloatSlice cFloatSlice;

Via the macros SLICE_DEF_C_ and SLICE_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

/* init */
cFloatSlice float_slice_c( int64_t s, float const* v );
cFloatSlice make_float_slice_c( float const* beg, float const* end );
cFloatSlice empty_float_slice_c( void );
/* sub */
cFloatSlice left_float_slice_c( cFloatSlice slice, int64_t maxLen );
cFloatSlice mid_float_slice_c( cFloatSlice slice, int64_t index );
cFloatSlice right_float_slice_c( cFloatSlice slice, int64_t maxLen );
cFloatSlice sub_float_slice_c( cFloatSlice slice,
                               int64_t begIdx,
                               int64_t endIdx );

cVarFloatSlice

struct cVarFloatSlice
{
   int64_t s;
   float* v;
};
typedef struct cVarFloatSlice cVarFloatSlice;

Via the macros SLICE_DEF_C_ and SLICE_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

/* init */
cVarFloatSlice var_float_slice_c( int64_t s, float* v );
cVarFloatSlice make_var_float_slice_c( float* beg, float* end );
cVarFloatSlice empty_var_float_slice_c( void );
/* sub */
cVarFloatSlice left_var_float_slice_c( cVarFloatSlice slice, int64_t maxLen );
cVarFloatSlice mid_var_float_slice_c( cVarFloatSlice slice, int64_t index );
cVarFloatSlice right_var_float_slice_c( cVarFloatSlice slice, int64_t maxLen );
cVarFloatSlice sub_var_float_slice_c( cVarFloatSlice slice,
                                      int64_t begIdx,
                                      int64_t endIdx );
/* var slice */
cFloatSlice as_float_slice_c( cVarFloatSlice slice );
cFloatSlice cast_float_slice_c( cVarFloatSlice slice, cFloatSlice sub );
int64_t set_float_slice_c( cVarFloatSlice dst, cFloatSlice src );

cFloatChunk

struct cFloatChunk
{
   int64_t s;
   float const* v;
   int64_t favSize;
   cFloatSlice slice;
};
typedef struct cFloatChunk cFloatChunk;

Via the macros CHUNK_DEF_C_ and CHUNK_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

void init_back_float_chunk_c( cFloatChunk chunk[static 1],
                              int64_t s,
                              cFloatSlice slice );
void init_front_float_chunk_c( cFloatChunk chunk[static 1],
                               int64_t s,
                               cFloatSlice slice );

cVarFloatChunk

struct cVarFloatChunk
{
   int64_t s;
   float* v;
   int64_t favSize;
   cVarFloatSlice slice;
};
typedef struc cVarFloatChunk cVarFloatChunk;

Via the macros CHUNK_DEF_C_ and CHUNK_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

void init_back_var_float_chunk_c( cVarFloatChunk chunk[static 1],
                                  int64_t s,
                                  cVarFloatSlice slice );
void init_front_var_float_chunk_c( cVarFloatChunk chunk[static 1],
                                   int64_t s,
                                   cVarFloatSlice slice );

cFloatWindow

struct cFloatWindow
{
   int64_t s;
   float const* v;
   int64_t favSize;
   cFloatSlice slice;
};
typedef struct cFloatWindow cFloatWindow;

Via the macros WINDOW_DEF_C_ and WINDOW_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

void init_back_float_window_c( cFloatWindow window[static 1],
                               int64_t s,
                               cFloatSlice slice );
void init_front_float_window_c( cFloatWindow window[static 1],
                                int64_t s,
                                cFloatSlice slice );

cVarFloatWindow

struct cVarFloatWindow
{
   int64_t s;
   float* v;
   int64_t favSize;
   cVarFloatSlice slice;
};
typedef struct cVarFloatWindow cVarFloatWindow;

Via the macros WINDOW_DEF_C_ and WINDOW_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

void init_back_var_float_window_c( cVarFloatWindow window[static 1],
                                   int64_t s,
                                   cVarFloatSlice slice );
void init_front_var_float_window_c( cVarFloatWindow window[static 1],
                                    int64_t s,
                                    cVarFloatSlice slice );

Functions

overall

cmp_float_c

int cmp_float_c( float a, float b );

Compares two float values and returns the three possible results:

<0

means that a is less compared to b

0

means that a and b are equal

>0

means that a is greater compared to b

Example
#include "clingo/lang/expect.h"
#include "clingo/type/float.h"

int main( void )
{
   init_tap_c_();

   float flt = -213.435f;
   float les = nextafterf( flt, -FLT_MAX );
   float gre = nextafterf( flt, FLT_MAX );

   expect_eq_c_( cmp_float_c( flt, flt ) );
   expect_lt_c_( cmp_float_c( les, flt ) );
   expect_gt_c_( cmp_float_c( gre, flt ) );

   return finish_tap_c_();
}

eq_float_c

#define eq_float_c_( F1, F2 )                                                  \
   eq_float_c( (F1), (F2), FLT_EPSILON )
bool eq_float_c( float f1, float f2, float epsilon );

Returns true if both value are equal if you allow a epsilon range.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/float.h"

int main( void )
{
   init_tap_c_();

   // macro with FLT_EPSILON
   expect_c_(  eq_float_c_( 0.123456f, 0.123456f ) );
   expect_c_( !eq_float_c_( 0.123456f, 0.123654f ) );

   // function with custom epsilon
   expect_c_( !eq_float_c( 0.123456f, 0.123654f, 0.000001f ) );
   expect_c_(  eq_float_c( 0.123456f, 0.123654f, 0.1f ) );

   return finish_tap_c_();
}

float_c_

#define float_c_( Value )

Macro function that casts the Value as float.

info

build_float_c

float build_float_c( cFloatInfo info );

Creates a float from a sign, exponent and mantissa value.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/float.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      uint8_t s;
      uint8_t e;
      uint32_t m;
      float exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( 1, 0x80, 0x48F5C3, -3.14f )
   );

   for_each_c_( test const*, t, tests )
   {
      cFloatInfo info = { t->s, t->e, t->m };
      float f = build_float_c( info );

      bool res = ( f == t->exp );

      tap_descf_c( res, "%x / %x / %x -> %f", t->s, t->e, t->m, t->exp );
   }

   return finish_tap_c_();
}

float_info_c

cFloatInfo float_info_c( float f );

Splits a float into a sign, exponent and mantissa value.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/float.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      float val;
      uint8_t s;
      uint8_t e;
      uint32_t m;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( 3.14f, 0, 0x80, 0x48F5C3 )
   );

   for_each_c_( test const*, t, tests )
   {
      cFloatInfo info = float_info_c( t->val );

      bool res = true;
      res &= info.sign == t->s;
      res &= info.exponent == t->e;
      res &= info.mantissa == t->m;

      tap_descf_c( res, "%f -> %x / %x / %x", t->val, t->s, t->e, t->m );
   }

   return finish_tap_c_();
}

swap

swap_float_c

float swap_float_c( float val );

Returns val with a changed byte order.

swap_float_from_c

float swap_float_from_c( float val, c_ByteOrder order );

Changes the byte order of val from a known order to the system order if necessary.

swap_float_to_c

float swap_float_to_c( float val, c_ByteOrder order );

Changes the byte order of val from the system order to a known order if necessary.

pack

pack_float_c

uint32_t pack_float_c( float f );

Packs the bytes of a float in a uint32_t value.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/float.h"

int main( void )
{
   init_tap_c_();

   float ordered = 2.9988165487136453e-38f;
   expect_c_( pack_float_c( ordered ) == 0x01234567 );

   float min = 1.4e-45f;
   expect_c_( pack_float_c( min ) == 0x00000001 );

   float max = 3.4028234e38f;
   expect_c_( pack_float_c( max ) == 0x7f7fffff );

   return finish_tap_c_();
}

unpack_float_c

float unpack_float_c( uint32_t u );

Unpacks a double from the bytes in a uint32_t value.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/float.h"

int main( void )
{
   init_tap_c_();

   uint32_t ordered = 0x01234567;
   expect_c_( unpack_float_c( ordered ) == 2.9988165487136453e-38f );

   uint32_t min_float32 = 0x00000001;
   expect_c_( unpack_float_c( min_float32 ) == 1.4e-45f );

   uint32_t max_float32 = 0x7f7fffff;
   expect_c_( unpack_float_c( max_float32 ) == 3.4028234e38f );

   return finish_tap_c_();
}

algo

find_float_c

float const* find_float_c( cFloatSlice slice, float f );

Via the macro FIND_VAL_C_ implemented function.

max_float_c

float const* max_float_c( cFloatSlice slice );

Via the macro FIND_MAX_C_ implemented function.

min_float_c

float const* min_float_c( cFloatSlice slice );

Via the macro FIND_MIN_C_ implemented function.

prod_float_c

bool prod_float_c( cFloatSlice slice, float res[static 1] );

Builds the product all values in the slice and saves the result in res. Return false if a overflow occures, otherwise true.

sum_float_c

bool sum_float_c( cFloatSlice slice, float res[static 1] );

Builds the sum of all values in the slice and saves the result in res. Returns false if a overflow occures, otherwise true.