Skip to content

Commit

Permalink
math: use memcpy to avoid losing precision
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Apr 26, 2024
1 parent 66261b0 commit 651ee53
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions include/cmetrics/cmt_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,30 @@

static inline uint64_t cmt_math_d64_to_uint64(double val)
{
uint64_t tmp;

if (isnan(val)) {
return 0;
/* we need to decide how to handle this */
}

if (isinf(val)) {
return UINT64_MAX;
/* we need to decide how to handle this */
}

if (val > (double) UINT64_MAX) {
if ((uint64_t) val > UINT64_MAX) {
return UINT64_MAX;
}

return (uint64_t) val;
memcpy(&tmp, &val, sizeof(double));
return (uint64_t) (tmp);
}

static inline double cmt_math_uint64_to_d64(uint64_t val)
{
return (double) val;
double tmp;

memcpy(&tmp, &val, sizeof(uint64_t));
return tmp;
}

#endif

0 comments on commit 651ee53

Please sign in to comment.