calc 0.5.0

CLI calculator app
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use crate::Value;

use super::{ArithmeticError, Error, Result};

impl Value {
    pub(crate) fn as_u32(self) -> Result<u32> {
        match self {
            Value::UnsignedInt(n) => u32::try_from(n).map_err(|_| ArithmeticError::Overflow.into()),
            Value::UnsignedBigInt(n) => {
                u32::try_from(n).map_err(|_| ArithmeticError::Overflow.into())
            }
            Value::SignedInt(n) => u32::try_from(n).map_err(|_| ArithmeticError::Overflow.into()),
            Value::SignedBigInt(n) => {
                u32::try_from(n).map_err(|_| ArithmeticError::Overflow.into())
            }
            Value::Float(n) => {
                if n < 0.0 {
                    return Err(ArithmeticError::Overflow.into());
                }
                if n.fract() != 0.0 {
                    return Err(Error::ImproperlyFloat);
                }
                // a 64-bit integer has at least enough precision to capture the integer part of this number
                let n = n as u64;

                u32::try_from(n).map_err(|_| ArithmeticError::Overflow.into())
            }
        }
    }

    /// Divide this value by another, flooring the result to the next lowest integer.
    pub fn trunc_div(mut self, other: impl Into<Self>) -> Self {
        self /= other;
        if let Value::Float(n) = &mut self {
            *n = n.floor();
        }
        self.demote();
        self
    }

    /// Raise this value by another.
    pub fn pow(self, right: impl Into<Value>) -> Result {
        let right = right.into();
        match self {
            Value::UnsignedInt(n) => {
                let right = right.as_u32()?;
                Ok(n.pow(right).into())
            }
            Value::UnsignedBigInt(n) => {
                let right = right.as_u32()?;
                Ok(n.pow(right).into())
            }
            Value::SignedInt(n) => {
                let right = right.as_u32()?;
                Ok(n.pow(right).into())
            }
            Value::SignedBigInt(n) => {
                let right = right.as_u32()?;
                Ok(n.pow(right).into())
            }
            Value::Float(n) => {
                if let Value::Float(e) = right {
                    Ok(n.powf(e).into())
                } else {
                    let right = right
                        .as_u32()?
                        .try_into()
                        .map_err(|_| ArithmeticError::Overflow)?;
                    Ok(n.powi(right).into())
                }
            }
        }
    }

    /// Compute the absolute value of this value.
    pub fn abs(self) -> Value {
        match self {
            Value::UnsignedInt(n) => n.into(),
            Value::UnsignedBigInt(n) => n.into(),
            Value::SignedInt(n) => n.abs().into(),
            Value::SignedBigInt(n) => n.abs().into(),
            Value::Float(n) => n.abs().into(),
        }
    }

    /// Compute the smallest integer greater than or equal to self.
    pub fn ceil(self) -> Value {
        if let Value::Float(n) = self {
            let mut out = Value::from(n.ceil());
            out.demote();
            out
        } else {
            self
        }
    }

    /// Compute the greatest integer less than or equal to self.
    pub fn floor(self) -> Value {
        if let Value::Float(n) = self {
            let mut out = Value::from(n.floor());
            out.demote();
            out
        } else {
            self
        }
    }

    /// Round self to the nearest integer; halfway cases away from 0.0.
    pub fn round(self) -> Value {
        if let Value::Float(n) = self {
            let mut out = Value::from(n.round());
            out.demote();
            out
        } else {
            self
        }
    }

    /// Compute the sine of self.
    pub fn sin(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.sin();
        }
        self
    }

    /// Compute the cosine of self.
    pub fn cos(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.cos();
        }
        self
    }

    /// Compute the tangent of self.
    pub fn tan(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.tan();
        }
        self
    }

    /// Compute the hyperbolic sine of self.
    pub fn sinh(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.sinh();
        }
        self
    }

    /// Compute the hyperbolic cosine of self.
    pub fn cosh(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.cosh();
        }
        self
    }

    /// Compute the hyperbolic tangent of self.
    pub fn tanh(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.tanh();
        }
        self
    }

    /// Compute the arcsine of self.
    pub fn asin(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.asin();
        }
        self
    }

    /// Compute the arccosine of self.
    pub fn acos(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.acos();
        }
        self
    }

    /// Compute the arctangent of self.
    pub fn atan(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.atan();
        }
        self
    }

    /// Compute the inverse hyperbolic sine of self.
    pub fn asinh(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.asinh();
        }
        self
    }

    /// Compute the inverse hyperbolic cosine of self.
    pub fn acosh(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.acosh();
        }
        self
    }

    /// Compute the inverse hyperbolic tangent of self.
    pub fn atanh(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.atanh();
        }
        self
    }

    /// Convert self as degrees to radians.
    pub fn rad(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f *= std::f64::consts::PI / 180.0;
        }
        self
    }

    /// Convert self as radians to degrees.
    pub fn deg(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f *= 180.0 / std::f64::consts::PI;
        }
        self
    }

    /// Determine the square root of self.
    pub fn sqrt(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.sqrt();
        }
        self
    }

    /// Determine the cube root of self.
    pub fn cbrt(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.cbrt();
        }
        self
    }

    /// Determine the base-10 logarithm of self.
    pub fn log(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.log10();
        }
        self
    }

    /// Determine the base-2 logarithm of self
    pub fn lg(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.log2();
        }
        self
    }

    /// Determine the base-`e` (natural) logarithm of self.
    pub fn ln(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.ln();
        }
        self
    }

    /// Determine `e**self`
    pub fn exp(mut self) -> Value {
        {
            let f = self.promote_to_float();
            *f = f.exp();
        }
        self
    }
}

#[cfg(test)]
mod demotion_tests {
    use super::*;
    use crate::value::Order;
    use rstest::rstest;

    // ---------- TRUNC_DIV ----------
    #[rstest]
    fn trunc_div_integers(
        #[values(10_u64, 20_u128, -30_i64, -40_i128)] left: impl Into<Value>,
        #[values(2_u64, 5_u128, -3_i64, -4_i128)] right: impl Into<Value>,
    ) {
        let left = left.into();
        let right = right.into();
        let result = left.trunc_div(right);
        // Integral inputs remain integral
        assert_ne!(result.order(), Order::Float);
    }

    #[rstest]
    fn trunc_div_float_demotes(
        #[values(10.5_f64, -20.9_f64, 1.0_f64)] left: f64,
        #[values(2.0_f64, -3.0_f64)] right: f64,
    ) {
        let left: Value = left.into();
        let right: Value = right.into();
        let result = left.trunc_div(right);
        // Floats demote to an integer type
        assert_ne!(result.order(), Order::Float);
    }

    // ---------- CEIL ----------
    #[rstest]
    fn ceil_integers_remain_integral(
        #[values(10_u64, 20_u128, -30_i64, -40_i128)] val: impl Into<Value>,
    ) {
        let val = val.into();
        let result = val.ceil();
        assert_eq!(result.order(), val.order());
    }

    #[rstest]
    fn ceil_float_demotes(#[values(1.2_f64, -1.8_f64, 1000.0_f64)] val: f64) {
        let val: Value = val.into();
        let result = val.ceil();
        assert_ne!(result.order(), Order::Float);
    }

    // ---------- FLOOR ----------
    #[rstest]
    fn floor_integers_remain_integral(
        #[values(10_u64, 20_u128, -30_i64, -40_i128)] val: impl Into<Value>,
    ) {
        let val = val.into();
        let result = val.floor();
        assert_eq!(result.order(), val.order());
    }

    #[rstest]
    fn floor_float_demotes(#[values(1.2_f64, -1.8_f64, 1000.0_f64)] val: f64) {
        let val: Value = val.into();
        let result = val.floor();
        assert_ne!(result.order(), Order::Float);
    }

    // ---------- ROUND ----------
    #[rstest]
    fn round_integers_remain_integral(
        #[values(10_u64, 20_u128, -30_i64, -40_i128)] val: impl Into<Value>,
    ) {
        let val = val.into();
        let result = val.round();
        assert_eq!(result.order(), val.order());
    }

    #[rstest]
    fn round_float_demotes(#[values(1.2_f64, 1.5_f64, -1.8_f64, -2.5_f64)] val: f64) {
        let val: Value = val.into();
        let result = val.round();
        assert_ne!(result.order(), Order::Float);
    }

    // ---------- EDGE CASES ----------
    #[test]
    fn ceil_of_large_float_demotes_to_bigint() {
        let val: Value = (u64::MAX as f64 * 1.5).into();
        let result = val.ceil();
        // Should require promotion to a big integer type
        assert!(matches!(result, Value::UnsignedBigInt(_)));
    }

    #[test]
    fn floor_of_negative_float_demotes_to_signed() {
        let val: Value = (-123.45_f64).into();
        let result = val.floor();
        assert!(matches!(result, Value::SignedInt(_)));
    }

    // ---------- FLOAT ÷ INT ----------
    #[rstest]
    fn trunc_div_float_div_int_demotes(
        #[values(10.9_f64, -20.1_f64, 1.5_f64)] left: f64,
        #[values(2_u64, 3_i64, 4_u128, -5_i128)] right: impl Into<Value>,
    ) {
        let left: Value = left.into();
        let right = right.into();
        let result = left.trunc_div(right);
        // Floats should demote to an integer type
        assert_ne!(result.order(), Order::Float);
    }

    // ---------- INT ÷ FLOAT ----------
    #[rstest]
    fn trunc_div_int_div_float_demotes(
        #[values(10_u64, -20_i64, 30_u128, -40_i128)] left: impl Into<Value>,
        #[values(2.5_f64, -3.5_f64, 1000.0_f64)] right: f64,
    ) {
        let left = left.into();
        let right: Value = right.into();
        let result = left.trunc_div(right);
        // Floats should demote to an integer type
        assert_ne!(result.order(), Order::Float);
    }

    // ---------- EDGE CASES ----------
    #[test]
    fn trunc_div_large_float_by_int_promotes_to_bigint() {
        let left: Value = (u64::MAX as f64 * 3.0).into();
        let right: Value = 2_u64.into();
        let result = left.trunc_div(right);
        // Result should require a big integer type
        assert!(matches!(result, Value::UnsignedBigInt(_)));
    }

    #[test]
    fn trunc_div_middling_large_float_by_int_promotes_to_bigint() {
        let left: Value = (u64::MAX as f64 * 1.5).into();
        let right: Value = 2_u64.into();
        let result = left.trunc_div(right);
        // Result should require a big integer type
        assert!(matches!(result, Value::UnsignedInt(_)));
    }

    #[test]
    fn trunc_div_negative_float_by_positive_int_is_signed() {
        let left: Value = (-123.45_f64).into();
        let right: Value = 2_u64.into();
        let result = left.trunc_div(right);
        assert!(matches!(result, Value::SignedInt(_)));
    }

    #[test]
    fn trunc_div_positive_int_by_negative_float_is_signed() {
        let left: Value = 123_u64.into();
        let right: Value = (-2.5_f64).into();
        let result = left.trunc_div(right);
        assert!(matches!(result, Value::SignedInt(_)));
    }
}