Type 7 – Floats & control tokens¶
This type combines two completely unrelated types of items – floating point numbers and special values such as true, false, null, etc. We refer to these special values as ‘control values’ or ‘ctrls’ for short throughout the code.
Just like integers, they have different possible width (resulting in different value ranges and precisions).
-
enum cbor_float_width¶
Possible widths of CBOR_TYPE_FLOAT_CTRL items.
Values:
-
enumerator CBOR_FLOAT_0¶
Internal use - ctrl and special values.
-
enumerator CBOR_FLOAT_16¶
Half float.
-
enumerator CBOR_FLOAT_32¶
Single float.
-
enumerator CBOR_FLOAT_64¶
Double.
-
enumerator CBOR_FLOAT_0¶
Corresponding |
|
Number of allocations |
One per lifetime |
Storage requirements |
|
Getting metadata¶
-
bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item)¶
Is this a ctrl value?
- param item[borrow]
A float or ctrl item
- return
Is this a ctrl value?
-
cbor_float_width cbor_float_get_width(const cbor_item_t *item)¶
Get the float width.
- param item[borrow]
A float or ctrl item
- return
The width.
Reading data¶
-
float cbor_float_get_float2(const cbor_item_t *item)¶
Get a half precision float.
The item must have the corresponding width
- param
-
float cbor_float_get_float4(const cbor_item_t *item)¶
Get a single precision float.
The item must have the corresponding width
- param
-
double cbor_float_get_float8(const cbor_item_t *item)¶
Get a double precision float.
The item must have the corresponding width
- param
-
double cbor_float_get_float(const cbor_item_t *item)¶
Get the float value represented as double.
Can be used regardless of the width.
- param
-
uint8_t cbor_ctrl_value(const cbor_item_t *item)¶
Reads the control value.
- param item[borrow]
A ctrl item
- return
the simple value
-
bool cbor_get_bool(const cbor_item_t *item)¶
Get value from a boolean ctrl item.
- param item[borrow]
A ctrl item
- return
boolean value
Creating new items¶
-
cbor_item_t *cbor_new_ctrl()¶
Constructs a new ctrl item.
The width cannot be changed once the item is created
- return
new 1B ctrl or
NULL
upon memory allocation failure
-
cbor_item_t *cbor_new_float2()¶
Constructs a new float item.
The width cannot be changed once the item is created
- return
new 2B float or
NULL
upon memory allocation failure
-
cbor_item_t *cbor_new_float4()¶
Constructs a new float item.
The width cannot be changed once the item is created
- return
new 4B float or
NULL
upon memory allocation failure
-
cbor_item_t *cbor_new_float8()¶
Constructs a new float item.
The width cannot be changed once the item is created
- return
new 8B float or
NULL
upon memory allocation failure
-
cbor_item_t *cbor_new_null()¶
Constructs new null ctrl item.
- return
new null ctrl item or
NULL
upon memory allocation failure
-
cbor_item_t *cbor_new_undef()¶
Constructs new undef ctrl item.
- return
new undef ctrl item or
NULL
upon memory allocation failure
Building items¶
-
cbor_item_t *cbor_build_bool(bool value)¶
Constructs new boolean ctrl item.
- param value
The value to use
- return
new boolean ctrl item or
NULL
upon memory allocation failure
-
cbor_item_t *cbor_build_ctrl(uint8_t value)¶
Constructs a ctrl item.
- param value
the value to use
- return
new ctrl item or
NULL
upon memory allocation failure
-
cbor_item_t *cbor_build_float2(float value)¶
Constructs a new float.
- param value
the value to use
- return
new float
-
cbor_item_t *cbor_build_float4(float value)¶
Constructs a new float.
- param value
the value to use
- return
new float or
NULL
upon memory allocation failure
-
cbor_item_t *cbor_build_float8(double value)¶
Constructs a new float.
- param value
the value to use
- return
new float or
NULL
upon memory allocation failure
Manipulating existing items¶
-
void cbor_set_ctrl(cbor_item_t *item, uint8_t value)¶
Assign a control value.
Warning
It is possible to produce an invalid CBOR value by assigning a invalid value using this mechanism. Please consult the standard before use.
- param item[borrow]
A ctrl item
- param value
The simple value to assign. Please consult the standard for allowed values
-
void cbor_set_bool(cbor_item_t *item, bool value)¶
Assign a boolean value to a boolean ctrl item.
- param item[borrow]
A ctrl item
- param value
The simple value to assign.
-
void cbor_set_float2(cbor_item_t *item, float value)¶
Assigns a float value.
- param item[borrow]
A half precision float
- param value
The value to assign
-
void cbor_set_float4(cbor_item_t *item, float value)¶
Assigns a float value.
- param item[borrow]
A single precision float
- param value
The value to assign
-
void cbor_set_float8(cbor_item_t *item, double value)¶
Assigns a float value.
- param item[borrow]
A double precision float
- param value
The value to assign
Half floats¶
CBOR supports two bytes wide (“half-precision”)
floats which are not supported by the C language. libcbor represents them using float <https://en.cppreference.com/w/c/language/type> values throughout the API. Encoding will be performed by cbor_encode_half()
, which will handle any values that cannot be represented as a half-float.