Numeric operations.
Overview
Numeric operations. More…
// typedefs typedef enum te_scalar_type te_scalar_type; // enums enum te_scalar_type; // global variables const te_enum_map te_scalar_type_names[]; // global functions uintmax_t te_scalar_type_max(te_scalar_type type); intmax_t te_scalar_type_min(te_scalar_type type); size_t te_scalar_type_sizeof(te_scalar_type type); bool te_scalar_type_is_signed(te_scalar_type type); te_errno te_scalar_type_fit_size(bool is_signed, size_t size, te_scalar_type* type); te_errno te_scalar_dynamic_cast(te_scalar_type src_type, const void* src, te_scalar_type dst_type, void* dst); te_errno te_double2int_safe(long double val, intmax_t lim, intmax_t* result); te_errno te_double2uint_safe(long double val, uintmax_t max, uintmax_t* result); // macros #define TE_SCALAR_TYPE_LIST(HANDLER_)
Detailed Documentation
Numeric operations.
Typedefs
typedef enum te_scalar_type te_scalar_type
Scalar types enumeration
Global Variables
const te_enum_map te_scalar_type_names[]
Maps te_scalar_type into a string name of corresponding C type
Global Functions
uintmax_t te_scalar_type_max(te_scalar_type type)
Get the maximum value of the given scalar type.
Parameters:
type |
Scalar type. |
Returns:
Maximum value of the scalar type
.
intmax_t te_scalar_type_min(te_scalar_type type)
Get the minimum value of the given scalar type.
Parameters:
type |
Scalar type. |
Returns:
Minimum value of the scalar type
.
size_t te_scalar_type_sizeof(te_scalar_type type)
Return the size of storage for values of given type (akin to sizeof
).
Parameters:
type |
Scalar type. |
Returns:
Size of type.
bool te_scalar_type_is_signed(te_scalar_type type)
Checks whether a type is signed or unsigned.
Parameters:
type |
Scalar type. |
Returns:
true
if the type
is signed.
te_errno te_scalar_type_fit_size(bool is_signed, size_t size, te_scalar_type* type)
Return a type spec than can hold values of given size.
Parameters:
is_signed |
Sign of value type. |
size |
Size of value type (the same as size of some scalar C type). |
type[out] |
Location for the scalar type. |
TE_ERANGE |
There is no scalar type of the requested size and sign. |
Returns:
Status code.
te_errno te_scalar_dynamic_cast(te_scalar_type src_type, const void* src, te_scalar_type dst_type, void* dst)
Cast the source value to the destination type.
If the src
value cannot be represented in the dst_type
exactly an error is returned. However, if the dst
is not NULL
, it is always overwritten by the casted src
even if src
value does not fit the range of dst_type
.
Parameters:
src_type |
Type of source value. |
src |
Pointer to memory holding the source value. |
dst_type |
Type of destination value. |
dst |
Location for the casted value (may be |
TE_EOVERFLOW |
Value does not fit the destination type. |
Returns:
Status code.
te_errno te_double2int_safe(long double val, intmax_t lim, intmax_t* result)
Safely converts a double val
to an integer.
The value is truncated towards zero, as with the regular conversion, but non-finite values and overflows are taken care of.
Parameters:
val |
input value |
lim |
limit value: the absolute of |
result |
output value |
0 |
Conversion is successful. |
TE_ERANGE |
The absolute value of |
TE_EDOM |
|
TE_EINVAL |
|
Returns:
status code
Macros
#define TE_SCALAR_TYPE_LIST(HANDLER_)
List of scalar types and their properties.
Each member of the list specifies one of the scalar types and describes its properties:
name of the scalar type: named constants are declared in the enum of scalar types;
corresponding type in C language;
minimum value of the scalar type (zero for unsigned types);
maximum value of the scalar type.
The properties are processed by a user-defined HANDLER_
which normally should be a local macro accepting at least four arguments, e.g.:
#define PRINT(scalar_type_, c_type_, min_, max_) \ printf("- %s %jd %ju", #c_type_, (min_), (max_)); TE_SCALAR_TYPE_LIST(PRINT) #undef PRINT
Refer https://en.wikipedia.org/wiki/X_macro for a description of the trick.
When adding new types to the list two constraints must be satisfied for te_scalar_type_fit_size() to work properly:
fixed-size types must precede semantic types;
fixed-size types must be ordered by their size.
Parameters:
HANDLER_ |
Name of the user-defined handler for members of the list |