Coercion¶
Coercing values to different datatypes.
-
typedef uint32_t ExessCoercions¶
Bitwise OR of
ExessCoercionflags.If this is zero, then only lossless coercions will be performed. A lossless coercion is when the value has been perfectly preserved in the target datatype, and coercing it back will result in the same value.
For some datatype combinations this will always be the case, for example from short to long. For others it will depend on the value, for example only the numbers 0 and 1 coerce to boolean without loss.
-
enum ExessCoercion¶
Coercion flags.
These values are ORed together to enable different kinds of lossy conversion.
-
enumerator EXESS_APPROXIMATE¶
Allow coercions that reduce the precision of values.
This allows coercions that are lossy only in terms of precision, so the resulting value is approximately equal to the original value. Specifically, this allows coercing double to float.
-
enumerator EXESS_ROUND¶
Allow coercions that round to the nearest integer.
This allows coercing floating point numbers to integers by rounding to the nearest integer, with halfway cases rounding towards even (the default IEEE-754 rounding order).
-
enumerator EXESS_TRUNCATE¶
Allow coercions that truncate significant parts of values.
Specifically, this allows coercing any number to boolean, and dateTime to date or time.
-
enumerator EXESS_APPROXIMATE¶
-
ExessResult exess_coerce_value(ExessCoercions coercions, ExessDatatype in_datatype, size_t in_size, const void *in, ExessDatatype out_datatype, size_t out_size, void *out)¶
Coerce a value to another datatype if possible.
- Parameters:
coercions – Enabled coercion flags. If this is zero, then
ExessStatus.EXESS_SUCCESSis only returned if the resulting value can be coerced back to the original type without any loss of data. Otherwise, the lossy coercions enabled by the set bits will be attempted.in_datatype – Datatype of
in.in_size – Size of
inin bytes.in – Input value to coerce.
out_datatype – Datatype to convert to.
out_size – Size of
outin bytes.out – Coerced value on success.
- Returns:
The
countof bytes written, and astatus:ExessStatus.EXESS_SUCCESSon successful conversion,ExessStatus.EXESS_OUT_OF_RANGEif the value is outside the range of the target type,ExessStatus.EXESS_LOSSif the required coercion isn’t enabled, orExessStatus.EXESS_UNSUPPORTEDif conversion between the types isn’t supported at all.