mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 10:14:06 +02:00
more percice scale float?
This commit is contained in:
parent
577a998baa
commit
d00c1a8f32
1 changed files with 21 additions and 5 deletions
|
|
@ -618,6 +618,11 @@ impl fmt::Display for SendPropValue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn float_scale(bit_count: u8) -> f32 {
|
||||||
|
// is this -1 correct?, it is consistent with the js version but seems weird
|
||||||
|
(1i32.wrapping_shl(bit_count as u32)) as f32 - 1.0
|
||||||
|
}
|
||||||
|
|
||||||
impl SendPropValue {
|
impl SendPropValue {
|
||||||
pub fn parse(stream: &mut Stream, definition: &SendPropParseDefinition) -> Result<Self> {
|
pub fn parse(stream: &mut Stream, definition: &SendPropParseDefinition) -> Result<Self> {
|
||||||
match definition {
|
match definition {
|
||||||
|
|
@ -768,9 +773,8 @@ impl SendPropValue {
|
||||||
high,
|
high,
|
||||||
} => {
|
} => {
|
||||||
let raw: u32 = stream.read_int(*bit_count as usize)?;
|
let raw: u32 = stream.read_int(*bit_count as usize)?;
|
||||||
// is this -1 correct?, it is consistent with the js version but seems weird
|
let scale = float_scale(*bit_count);
|
||||||
let percentage =
|
let percentage = (raw as f32) / scale;
|
||||||
(raw as f32) / ((1i32.wrapping_shl(*bit_count as u32)) as f32 - 1.0);
|
|
||||||
Ok(low + ((high - low) * percentage))
|
Ok(low + ((high - low) * percentage))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -802,8 +806,8 @@ impl SendPropValue {
|
||||||
high,
|
high,
|
||||||
} => {
|
} => {
|
||||||
let percentage = (val - low) / (high - low);
|
let percentage = (val - low) / (high - low);
|
||||||
let raw =
|
let scale = float_scale(*bit_count);
|
||||||
(percentage * ((1i32.wrapping_shl(*bit_count as u32)) as f32 - 1.0)) as u32;
|
let raw = (percentage * scale).round() as u32;
|
||||||
raw.write_sized(stream, *bit_count as usize)?;
|
raw.write_sized(stream, *bit_count as usize)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -926,6 +930,18 @@ fn test_send_prop_value_roundtrip() {
|
||||||
count_bit_count: 5,
|
count_bit_count: 5,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
send_prop_value_roundtrip(
|
||||||
|
SendPropValue::Float(76.22549),
|
||||||
|
SendPropParseDefinition::Float {
|
||||||
|
changes_often: false,
|
||||||
|
definition: FloatDefinition::Scaled {
|
||||||
|
bit_count: 10,
|
||||||
|
high: 102.3,
|
||||||
|
low: 0.09990235,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<i32> for SendPropValue {
|
impl From<i32> for SendPropValue {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue