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

Simplify DigitalStorage and FlopCount units

Kasper (Jun 24, 2026, 2:13 PM +0200) 09fbd9ac b9ee8b3d

+46
+46
src/units.rs
··· 646 646 } else { 647 647 return Number::with_basic_unit(value / Millivolt.weight(), Millivolt); 648 648 } 649 + } else if primitive == DigitalStorage.primitive() { 650 + let bits = &[ 651 + Bit, Kilobit, Megabit, Gigabit, Terabit, Petabit, Exabit, Zettabit, Yottabit, 652 + ]; 653 + let bibits = &[ 654 + Bit, Kibibit, Mebibit, Gibibit, Tebibit, Pebibit, Exbibit, Zebibit, Yobibit, 655 + ]; 656 + let bytes = &[ 657 + Byte, Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte, Exabyte, Zettabyte, Yottabyte, 658 + ]; 659 + let bibytes = &[ 660 + Byte, Kibibyte, Mebibyte, Gibibyte, Tebibyte, Pebibyte, Exbibyte, Zebibyte, Yobibyte, 661 + ]; 662 + for unit in &number.unit { 663 + if unit.0.category() == DigitalStorage || unit.0.category() == DataTransferRate { 664 + let weight = unit.0.weight(); 665 + let candidates = if weight % 8192 == d!(0) { 666 + bibytes 667 + } else if weight % 8000 == d!(0) { 668 + bytes 669 + } else if weight % 1024 == d!(0) { 670 + bibits 671 + } else { 672 + bits 673 + }; 674 + let unit = candidates 675 + .iter() 676 + .rev() 677 + .find(|&&u| value >= u.weight()) 678 + .copied() 679 + .unwrap_or(candidates[0]); 680 + return Number::with_basic_unit(value / unit.weight(), unit); 681 + } 682 + } 683 + } else if primitive == FlopCount.primitive() { 684 + let candidates = [ 685 + Flop, KiloFlop, MegaFlop, GigaFlop, TeraFlop, PetaFlop, ExaFlop, ZettaFlop, YottaFlop, 686 + RonnaFlop, QuettaFlop, 687 + ]; 688 + let unit = candidates 689 + .iter() 690 + .rev() 691 + .find(|&&u| value >= u.weight()) 692 + .copied() 693 + .unwrap_or(candidates[0]); 694 + return Number::with_basic_unit(value / unit.weight(), unit); 649 695 } 650 696 number 651 697 }