pub unsafe extern "C" fn phydat_fit(
dat: *mut phydat_t,
values: *const i32,
dim: c_uint,
)
Expand description
@brief Scale integer value(s) to fit into a @ref phydat_t
Inserts the @p values in the given @p dat so that all @p dim values in
@p values fit inside the limits of the data type,
[@ref PHYDAT_MIN, @ref PHYDAT_MAX], and updates the stored scale factor.
The @ref phydat_t::scale member in @p dat is used as the the original scale
of the @p values.
The value is rounded to the nearest integer if possible, otherwise away from
zero. E.g. 0.5
and 0.6
are rounded to 1
, 0.4
and -0.4
are rounded
to 0
, -0.5
and -0.6
are rounded to -1
.
int32_t values[] = { 100000, 2000000, 30000000 };
phydat_t dat = { .scale = 0 };
phydat_fit(&dat, values, 3);
@note Unless compiled with -DPHYDAT_FIT_TRADE_PRECISION_FOR_ROM=0
, this
function will scale the value -32768
, even though it would fit into a
@ref phydat_t. Statistically, this precision loss happens in 0.00153%
of the calls. This optimization saves a bit more than 20 bytes.
@pre The @ref phydat_t::scale member in @p dat is initialized to the scale of the @p values by the caller prior to calling this function.
@param[in, out] dat the value will be written into this data array @param[in] values value(s) to rescale @param[in] dim Number of elements in @p values