Skip to content

Commit

Permalink
Implement float_to_int() function
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Jun 15, 2024
1 parent 6f19fd6 commit 1b71a81
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/tree/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Convert unsigned 32-bit integer counter to floating point (-1 to 1)
/// Convert signed 32-bit integer counter to floating point (-1 to 1)
pub(crate) fn int_to_float(int: i32) -> f32 {

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, beta)

function `int_to_float` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, stable)

function `int_to_float` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, nightly)

function `int_to_float` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

function `int_to_float` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly)

function `int_to_float` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

function `int_to_float` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, nightly)

function `int_to_float` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

function `int_to_float` is never used

Check warning on line 2 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, stable)

function `int_to_float` is never used
// Split sign and magnitude from signed integer
let (uint, sign_bit) = if int < 0 {
Expand Down Expand Up @@ -32,6 +32,21 @@ pub(crate) fn int_to_float(int: i32) -> f32 {
f32::from_bits(sign_bit | exponent | fraction)
}

/// Convert floating point (-1 to 1) to signed 32-bit integer
pub(crate) fn float_to_int(float: f32) -> i32 {

Check warning on line 36 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, beta)

function `float_to_int` is never used

Check warning on line 36 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, stable)

function `float_to_int` is never used

Check warning on line 36 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, nightly)

function `float_to_int` is never used

Check warning on line 36 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, beta)

function `float_to_int` is never used

Check warning on line 36 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly)

function `float_to_int` is never used

Check warning on line 36 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

function `float_to_int` is never used

Check warning on line 36 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, nightly)

function `float_to_int` is never used

Check warning on line 36 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, beta)

function `float_to_int` is never used

Check warning on line 36 in src/tree/conversions.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest, stable)

function `float_to_int` is never used
let float = float.to_bits();
let sign = (float & (1 << 31)) >> 31;
let int = ((float << 9) >> 9) | (1 << 24);
let exponent = (float << 1) >> 24;
let shift = std::dbg!(127u32.saturating_sub(exponent));

if shift >= 32 {
0
} else {
((int as i32) * (1 - (sign as i32 * 2))).saturating_mul(128) >> shift
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -56,5 +71,23 @@ mod tests {
assert_eq!(int_to_float(i32::MIN / 4), -0.25);
assert_eq!(int_to_float(i32::MIN / 2), -0.5);
assert_eq!(int_to_float(i32::MIN), -1.0);

assert_eq!(float_to_int(0.0), 0);
assert_eq!(float_to_int(-1.0), i32::MIN);
assert_eq!(float_to_int(-0.5), i32::MIN / 2);
assert_eq!(float_to_int(-0.25), i32::MIN / 4);
//assert_eq!(float_to_int(4.1909516e-9), 4);
assert_eq!(float_to_int(3.259629e-9), 3);
//assert_eq!(float_to_int(2.3283064e-9), 2);
assert_eq!(float_to_int(1.3969839e-9), 1);
assert_eq!(float_to_int(2.0f32.powf(-31.0)), 0);
assert_eq!(float_to_int(-2.0f32.powf(-31.0)), -1);
assert_eq!(float_to_int(-1.3969839e-9), -2);
//assert_eq!(float_to_int(-2.3283064e-9), -3);
assert_eq!(float_to_int(-3.259629e-9), -4);
//assert_eq!(float_to_int(-4.1909516e-9), -5);
assert_eq!(float_to_int(0.25), i32::MAX / 4);
assert_eq!(float_to_int(0.5), i32::MAX / 2);
assert_eq!(float_to_int(1.0), i32::MAX);
}
}

0 comments on commit 1b71a81

Please sign in to comment.