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 macro SLICES_C_ generated struct.
cVarFloatSlice
struct cVarFloatSlice
{
int64_t s;
float* v;
};
typedef struct cVarFloatSlice cVarFloatSlice;
Via the macro SLICES_C_ generated struct.
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
#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.
#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.
#include "clingo/lang/expect.h"
#include "clingo/type/float.h"
TEMP_SLICE_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.
#include "clingo/lang/expect.h"
#include "clingo/type/float.h"
TEMP_SLICE_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.
#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.
#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.