Skip to content

Commit

Permalink
Add helper for resistor_calc "fixed point" error values
Browse files Browse the repository at this point in the history
  • Loading branch information
twelho committed Jun 24, 2021
1 parent f950a80 commit aeb3f3a
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tools/kicad/kicad_functions/src/vdiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub(crate) fn voltage_divider(argument: &Value) -> EvalexprResult<Value> {
if let Some(res) = calculate(&config) {
// Take the first result, these are ordered by increasing error
if let Some((v, set)) = res.iter().next() {
let voltage = config.target + *v as f64 / 1e9;
let voltage = config.target + fixed_to_floating(*v);
let mut tuple = vec![Value::from(voltage)];
for i in 1..=config.count {
tuple.push(Value::from(set.r(i)));
Expand All @@ -128,3 +128,10 @@ pub(crate) fn voltage_divider(argument: &Value) -> EvalexprResult<Value> {

err(&format!("no solution found: {}", argument))
}

// resistor_calc outputs error quantities as "fixed point" numbers by multiplying
// a float by 1e9, rounding the result and converting to u64. We need to undo
// that procedure here.
fn fixed_to_floating(value: u64) -> f64 {
(value as f64) / 1e9
}

0 comments on commit aeb3f3a

Please sign in to comment.