[READ-ONLY] Mirror of https://github.com/probablykasper/cpc. Text calculator with support for units and conversion cpc.kasper.space
calculator cli conversion converter library math package units units-converter wasm website
0

Configure Feed

Select the types of activity you want to include in your feed.

Add convert_to_lowest()

Kasper (Jun 24, 2026, 8:54 AM +0200) 449339b0 267a2031

+10 -10
+10 -10
src/units.rs
··· 468 468 /// If one of two provided [`Number`]s has a larger [`Unit`] than the other, convert 469 469 /// the large one to the unit of the small one. 470 470 pub fn convert_to_lowest(left: Number, right: Number) -> Result<(Number, Number), String> { 471 - todo!(); 472 - // if left.unit.weight() == right.unit.weight() { 473 - // Ok((left, right)) 474 - // } else if left.unit.weight() > right.unit.weight() { 475 - // let left_converted = convert(left, right.unit)?; 476 - // Ok((left_converted, right)) 477 - // } else { 478 - // let right_converted = convert(right, left.unit)?; 479 - // Ok((left, right_converted)) 480 - // } 471 + assert!(left.primitive_unit() == right.primitive_unit()); 472 + if combined_weight(&left.unit) == combined_weight(&right.unit) { 473 + Ok((left, right)) 474 + } else if combined_weight(&left.unit) > combined_weight(&right.unit) { 475 + let left_converted = convert(left, right.unit.clone())?; 476 + Ok((left_converted, right)) 477 + } else { 478 + let right_converted = convert(right, left.unit.clone())?; 479 + Ok((left, right_converted)) 480 + } 481 481 } 482 482 483 483 /// Return the sum of two [`Number`]s