[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.

Improve error messages

Kasper (Jun 24, 2026, 9:42 AM +0200) 27c219ab e19f2972

+22 -28
+5 -5
src/evaluator.rs
··· 75 75 let result = cbrt(child_answer.value); 76 76 Ok(Number::with_unit(result, child_answer.unit)) 77 77 } else { 78 - Err("log() only accepts UnitType::NoType".to_string()) 78 + Err("cbrt() only accepts unitless numbers".to_string()) 79 79 } 80 80 } 81 81 Sqrt => { ··· 83 83 let result = sqrt(child_answer.value); 84 84 Ok(Number::with_unit(result, child_answer.unit)) 85 85 } else { 86 - Err("log() only accepts UnitType::NoType".to_string()) 86 + Err("sqrt() only accepts unitless numbers".to_string()) 87 87 } 88 88 } 89 89 Log => { ··· 91 91 let result = child_answer.value.log10(); 92 92 Ok(Number::with_unit(result, child_answer.unit)) 93 93 } else { 94 - Err("log() only accepts UnitType::NoType".to_string()) 94 + Err("log() only accepts unitless numbers".to_string()) 95 95 } 96 96 } 97 97 Ln => { ··· 99 99 let result = child_answer.value.ln(); 100 100 Ok(Number::with_unit(result, child_answer.unit)) 101 101 } else { 102 - Err("ln() only accepts UnitType::NoType".to_string()) 102 + Err("ln() only accepts unitless numbers".to_string()) 103 103 } 104 104 } 105 105 Exp => { ··· 107 107 let result = child_answer.value.exp(); 108 108 Ok(Number::with_unit(result, child_answer.unit)) 109 109 } else { 110 - Err("exp() only accepts UnitType::NoType".to_string()) 110 + Err("exp() only accepts unitless numbers".to_string()) 111 111 } 112 112 } 113 113 Round => {
+17 -23
src/units.rs
··· 397 397 pub fn convert(number: Number, to_unit: Vec<(Unit, isize)>) -> Result<Number, String> { 398 398 if number.primitive_unit() != primitive_unit(&to_unit) { 399 399 return Err(format!( 400 - "Cannot convert from {:?} to {:?}", 401 - number.unit, to_unit 400 + "Cannot convert {} to {}", 401 + number, 402 + Number::with_unit(d!(0), to_unit).plural() 402 403 )); 403 404 } 404 405 let value = number.value; 405 - if number.primitive_unit() == vec![(Kelvin, 1)] { 406 + if number.primitive_unit() == Temperature.primitive() { 406 407 if number.unit.len() != 1 407 408 || to_unit.len() != 1 408 409 || number.unit[0].1 != 1 409 410 || to_unit[0].1 != 1 410 411 { 411 412 return Err(format!( 412 - "Cannot convert from {:?} to {:?}", 413 - number.unit, to_unit 413 + "Cannot convert {} to {}", 414 + number, 415 + Number::with_unit(d!(0), to_unit).plural() 414 416 )); 415 417 } 416 418 let ok = |new_value| Ok(Number::with_unit(new_value, to_unit.clone())); ··· 425 427 (Fahrenheit, Kelvin) => ok((value + d!(459.67)) * d!(5) / d!(9)), 426 428 (Fahrenheit, Celsius) => ok((value - d!(32)) / d!(1.8)), 427 429 _ => Err(format!( 428 - "Error converting temperature {:?} to {:?}", 429 - number.unit, to_unit 430 + "Error converting temperature {} to {}", 431 + number, 432 + Number::with_unit(d!(0), to_unit).plural() 430 433 )), 431 434 } 432 435 } else { ··· 465 468 let (left, right) = convert_to_lowest(left, right)?; 466 469 Ok(Number::with_unit(left.value + right.value, left.unit)) 467 470 } else { 468 - Err(format!("Cannot add {:?} and {:?}", left.unit, right.unit)) 471 + Err(format!("Cannot add {} and {}", left, right)) 469 472 } 470 473 } 471 474 ··· 479 482 let (left, right) = convert_to_lowest(left, right)?; 480 483 Ok(Number::with_unit(left.value - right.value, left.unit)) 481 484 } else { 482 - Err(format!( 483 - "Cannot subtract {:?} by {:?}", 484 - left.unit, right.unit 485 - )) 485 + Err(format!("Cannot subtract {} by {}", left, right)) 486 486 } 487 487 } 488 488 ··· 636 636 /// Temperatures don't work 637 637 pub fn multiply(left: Number, right: Number) -> Result<Number, String> { 638 638 if left.contains_primitive(Temperature) || right.contains_primitive(Temperature) { 639 - Err(format!( 640 - "Cannot multiply {:?} and {:?}", 641 - left.unit, right.unit 642 - )) 639 + Err(format!("Cannot multiply {} and {}", left, right)) 643 640 } else { 644 641 let mut new_number = left; 645 642 new_number.value *= right.value; ··· 736 733 /// Temperatures don't work. 737 734 pub fn divide(left: Number, right: Number) -> Result<Number, String> { 738 735 if left.contains_primitive(Temperature) || right.contains_primitive(Temperature) { 739 - Err(format!("Cannot divide {:?} by {:?}", left.unit, right.unit)) 736 + Err(format!("Cannot divide {} by {}", left, right)) 740 737 } else { 741 738 let mut new_number = left; 742 739 new_number.value /= right.value; ··· 759 756 /// Temperatures don't work. 760 757 pub fn modulo(left: Number, right: Number) -> Result<Number, String> { 761 758 if left.contains_primitive(Temperature) || right.contains_primitive(Temperature) { 762 - Err(format!("Cannot modulo {:?} by {:?}", left.unit, right.unit)) 759 + Err(format!("Cannot modulo {} by {}", left, right)) 763 760 } else if left.primitive_unit() == right.primitive_unit() { 764 761 // 5 km % 3 m 765 762 let (left, right) = convert_to_lowest(left, right)?; 766 763 Ok(Number::with_unit(left.value % right.value, left.unit)) 767 764 } else { 768 - Err(format!("Cannot modulo {:?} by {:?}", left.unit, right.unit)) 765 + Err(format!("Cannot modulo {} by {}", left, right)) 769 766 } 770 767 } 771 768 ··· 778 775 pub fn pow(left: Number, right: Number) -> Result<Number, String> { 779 776 // I tried converting `right` to use powi, but somehow that was slower 780 777 if left.contains_primitive(Temperature) || right.has_unit() { 781 - Err(format!( 782 - "Cannot raise {:?} to the power of {:?}", 783 - left.unit, right.unit 784 - )) 778 + Err(format!("Cannot raise {} to the power of {}", left, right)) 785 779 } else if left.is_unitless() { 786 780 let result = left.value.pow(right.value); 787 781 let new_number = Number::new_unitless(result);