launchdarkly_server_sdk_evaluation/util.rs
1const FLOAT_TO_INT_MAX: f64 = 9007199254740991_f64;
2
3/// Converting float to int has undefined behaviour for huge floats: https://stackoverflow.com/a/41139453.
4/// To avoid this, refuse to convert floats with magnitude greater than 2**53 - 1, after which 64-bit floats no longer
5/// retain integer precision. We could go a few orders of magnitude higher without triggering the UB, but this seems like
6/// the least surprising place to put a breakpoint.
7pub(crate) fn f64_to_i64_safe(f: f64) -> Option<i64> {
8 if f.abs() <= FLOAT_TO_INT_MAX {
9 Some(f as i64)
10 } else {
11 None
12 }
13}
14
15pub(crate) fn is_false(b: &bool) -> bool {
16 !(*b)
17}