| | | @@ 変更前 1行目〜 / 変更後 1行目〜 @@ |
| 1 | 1 | | //! データ通信量(ギガ)の目安。 |
| 2 | | − | //! v1: よくある書き方(f64 で計算し、format! の {:.0}・{:.1} と f64::round で表示)。 |
| 2 | + | //! v2: 入力を i128 の分数(分子・分母)のまま計算し、境目の判定と四捨五入(0.5 は繰り上げ)を整数で行う。 |
| 3 | + | //! f64 だと 0.08 × 3.5 × 25 が 7.000000000000001 になって 10GB プランに上がり、format! の {:.0} は 12.5 を偶数丸めで 12 にする。 |
| 3 | 4 | | |
| 4 | 5 | | use crate::json::{obj, s, Value}; |
| 5 | 6 | | |
| 6 | | − | const TIERS: [f64; 8] = [3.0, 5.0, 7.0, 10.0, 20.0, 30.0, 50.0, 100.0]; |
| 7 | + | const TIERS: [i128; 8] = [3, 5, 7, 10, 20, 30, 50, 100]; |
| 7 | 8 | | |
| 8 | | − | fn fmt_data(gb: f64) -> String { |
| 9 | | − | if gb >= 10.0 { |
| 10 | | − | return format!("{gb:.0} GB"); |
| 11 | | − | } |
| 12 | | − | if gb >= 1.0 { |
| 13 | | − | return format!("{gb:.1} GB"); |
| 14 | | − | } |
| 15 | | − | let mb = gb * 1024.0; |
| 16 | | − | if mb >= 100.0 { format!("{} MB", mb.round() as i64) } else { format!("{mb:.1} MB") } |
| 9 | + | /// 分数 p / q(q > 0、約分済み) |
| 10 | + | #[derive(Clone, Copy)] |
| 11 | + | struct Frac(i128, i128); |
| 12 | + | |
| 13 | + | fn gcd(a: i128, b: i128) -> i128 { |
| 14 | + | if b == 0 { a.abs() } else { gcd(b, a % b) } |
| 17 | 15 | | } |
| 18 | 16 | | |
| 19 | | − | fn fmt_hours(h: f64) -> String { |
| 20 | | − | if h >= 100.0 { |
| 21 | | − | return format!("{} 時間", h.round() as i64); |
| 17 | + | impl Frac { |
| 18 | + | fn new(p: i128, q: i128) -> Frac { |
| 19 | + | let g = gcd(p, q).max(1); |
| 20 | + | let sg = if q < 0 { -1 } else { 1 }; |
| 21 | + | Frac(sg * p / g, sg * q / g) |
| 22 | 22 | | } |
| 23 | | − | let mut whole = h.floor() as i64; |
| 24 | | − | let mut mins = ((h - whole as f64) * 60.0).round() as i64; |
| 23 | + | /// "0.08" → 2/25 |
| 24 | + | fn parse(t: &str) -> Result<Frac, String> { |
| 25 | + | let (int, frac) = t.split_once('.').unwrap_or((t, "")); |
| 26 | + | if frac.len() > 18 || !frac.bytes().all(|b| b.is_ascii_digit()) { |
| 27 | + | return Err(format!("invalid decimal: {t}")); |
| 28 | + | } |
| 29 | + | let p: i128 = format!("{int}{frac}").parse().map_err(|_| format!("invalid decimal: {t}"))?; |
| 30 | + | Ok(Frac::new(p, 10i128.pow(frac.len() as u32))) |
| 31 | + | } |
| 32 | + | fn int(k: i128) -> Frac { |
| 33 | + | Frac(k, 1) |
| 34 | + | } |
| 35 | + | fn mul(self, o: Frac) -> Frac { |
| 36 | + | Frac::new(self.0 * o.0, self.1 * o.1) |
| 37 | + | } |
| 38 | + | fn div(self, o: Frac) -> Frac { |
| 39 | + | Frac::new(self.0 * o.1, self.1 * o.0) |
| 40 | + | } |
| 41 | + | fn sub(self, o: Frac) -> Frac { |
| 42 | + | Frac::new(self.0 * o.1 - o.0 * self.1, self.1 * o.1) |
| 43 | + | } |
| 44 | + | /// self >= k(整数) |
| 45 | + | fn ge(self, k: i128) -> bool { |
| 46 | + | self.0 >= k * self.1 |
| 47 | + | } |
| 48 | + | /// 0 以上の値の切り捨て |
| 49 | + | fn floor(self) -> i128 { |
| 50 | + | self.0 / self.1 |
| 51 | + | } |
| 52 | + | /// 0 以上の値を整数に四捨五入(0.5 は繰り上げ)= floor((2p + q) / 2q) |
| 53 | + | fn half_up(self) -> i128 { |
| 54 | + | (2 * self.0 + self.1) / (2 * self.1) |
| 55 | + | } |
| 56 | + | } |
| 57 | + | |
| 58 | + | fn one_decimal(x: Frac) -> String { |
| 59 | + | let t = x.mul(Frac::int(10)).half_up(); |
| 60 | + | format!("{}.{}", t / 10, t % 10) |
| 61 | + | } |
| 62 | + | |
| 63 | + | fn fmt_data(gb: Frac) -> String { |
| 64 | + | if gb.ge(10) { |
| 65 | + | return format!("{} GB", gb.half_up()); |
| 66 | + | } |
| 67 | + | if gb.ge(1) { |
| 68 | + | return format!("{} GB", one_decimal(gb)); |
| 69 | + | } |
| 70 | + | let mb = gb.mul(Frac::int(1024)); |
| 71 | + | if mb.ge(100) { format!("{} MB", mb.half_up()) } else { format!("{} MB", one_decimal(mb)) } |
| 72 | + | } |
| 73 | + | |
| 74 | + | fn fmt_hours(h: Frac) -> String { |
| 75 | + | if h.ge(100) { |
| 76 | + | return format!("{} 時間", h.half_up()); |
| 77 | + | } |
| 78 | + | let mut whole = h.floor(); |
| 79 | + | let mut mins = h.sub(Frac::int(whole)).mul(Frac::int(60)).half_up(); |
| 25 | 80 | | if mins == 60 { |
| 26 | 81 | | whole += 1; |
| 27 | 82 | | mins = 0; |
| | | @@ 変更前 35行目〜 / 変更後 90行目〜 @@ |
| 35 | 90 | | } |
| 36 | 91 | | } |
| 37 | 92 | | |
| 38 | | − | fn num(t: &str) -> Result<f64, String> { |
| 39 | | − | t.parse::<f64>().map_err(|e| format!("{e}")) |
| 40 | | − | } |
| 41 | | − | |
| 42 | 93 | | pub fn calc(per_hour: &str, hours: &str, days: &str) -> Result<Value, String> { |
| 43 | | − | let (p, h, d) = (num(per_hour)?, num(hours)?, num(days)?); |
| 44 | | − | let monthly = p * h * d; |
| 45 | | − | let plan = match TIERS.iter().find(|&&t| monthly <= t) { |
| 94 | + | let (p, h, d) = (Frac::parse(per_hour)?, Frac::parse(hours)?, Frac::parse(days)?); |
| 95 | + | if p.0 <= 0 { |
| 96 | + | return Err(format!("per_hour must be positive: {per_hour}")); |
| 97 | + | } |
| 98 | + | let monthly = p.mul(h).mul(d); |
| 99 | + | let plan = match TIERS.iter().find(|&&t| monthly.0 <= t * monthly.1) { |
| 46 | 100 | | Some(t) => format!("{t}GB プランが目安"), |
| 47 | 101 | | None => "大容量・無制限プランが目安".to_string(), |
| 48 | 102 | | }; |
| 49 | 103 | | Ok(obj(vec![ |
| 50 | 104 | | ("monthly", s(fmt_data(monthly))), |
| 51 | | − | ("per_day", s(fmt_data(p * h))), |
| 52 | | − | ("hours_3gb", s(fmt_hours(3.0 / p))), |
| 53 | | − | ("hours_20gb", s(fmt_hours(20.0 / p))), |
| 105 | + | ("per_day", s(fmt_data(p.mul(h)))), |
| 106 | + | ("hours_3gb", s(fmt_hours(Frac::int(3).div(p)))), |
| 107 | + | ("hours_20gb", s(fmt_hours(Frac::int(20).div(p)))), |
| 54 | 108 | | ("plan", s(plan)), |
| 55 | 109 | | ])) |
| 56 | 110 | | } |