main.rs
6.21 KB
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
//
// Test our fractional crate / module...
//
// Georg Hopp <georg@steffers.org>
//
// Copyright © 2019 Georg Hopp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
use std::convert::{TryFrom, TryInto};
use std::num::TryFromIntError;
use std::f64::consts::PI as FPI;
use fractional::fractional::{Fractional, from_vector};
use fractional::trigonometry::{sin, cos, PI};
struct Vector {
x: Fractional,
y: Fractional,
z: Fractional,
}
fn mean(v: &Vec<i64>) -> Result<Fractional, TryFromIntError> {
let r = v.iter().fold(0, |acc, x| acc + x);
let l = i64::try_from(v.len())?;
Ok(Fractional(r, l))
}
fn main() {
let a = vec![3, 6, 1, 9];
let b = from_vector(&a);
let c = mean(&a).unwrap(); // This might fail if the len of the
// vector (usize) does not fit into i32.
let d :f64 = c.try_into().unwrap();
let e :f64 = Fractional::try_into(c).unwrap();
let f = Fractional(9, 4);
let g = Fractional(-9, 4);
println!(" [i32] : {:?}" , a);
println!(" [Fractional] : {:?}" , b);
println!(" mean of [i32] : {}" , c);
println!(" as f64 : {}" , d);
println!(" and as f64 : {}" , e);
println!(" again as f64 : {}" , TryInto::<f64>::try_into(c).unwrap());
println!(" sqrt f : {}" , f.sqrt());
println!(" sqrt f as f64 : {}" , TryInto::<f64>::try_into(f.sqrt()).unwrap());
println!(" sqrt g : {}" , g.sqrt());
println!(" sqrt g as f64 : {}" , TryInto::<f64>::try_into(g.sqrt()).unwrap());
println!(" Rust π : {}" , FPI);
println!(" π : {} {}" , TryInto::<f64>::try_into(PI).unwrap(), PI);
println!(" π as tuple : {:?}" , TryInto::<(i32, i32)>::try_into(PI).unwrap());
println!(" Rust π² : {}" , FPI * FPI);
println!(" π² : {} {}" , TryInto::<f64>::try_into(PI * PI).unwrap(), PI * PI);
println!(" sin 9 : {}" , sin(9));
println!(" sin 9 : {}" , TryInto::<f64>::try_into(sin(9)).unwrap());
println!(" Rust sin 9 : {}" , f64::sin(9.0 * FPI / 180.0));
println!(" sin 17 : {}" , sin(17));
println!(" sin 17 : {}" , TryInto::<f64>::try_into(sin(17)).unwrap());
println!(" Rust sin 17 : {}" , f64::sin(17.0 * FPI / 180.0));
println!(" sin 31 : {}" , sin(31));
println!(" sin 31 : {}" , TryInto::<f64>::try_into(sin(31)).unwrap());
println!(" Rust sin 31 : {}" , f64::sin(31.0 * FPI / 180.0));
println!(" sin 45 : {}" , sin(45));
println!(" sin 45 : {}" , TryInto::<f64>::try_into(sin(45)).unwrap());
println!(" Rust sin 45 : {}" , f64::sin(45.0 * FPI / 180.0));
println!(" sin 73 : {}" , sin(73));
println!(" sin 73 : {}" , TryInto::<f64>::try_into(sin(73)).unwrap());
println!(" Rust sin 73 : {}" , f64::sin(73.0 * FPI / 180.0));
println!(" sin 123 : {}" , sin(123));
println!(" sin 123 : {}" , TryInto::<f64>::try_into(sin(123)).unwrap());
println!(" Rust sin 123 : {}" , f64::sin(123.0 * FPI / 180.0));
println!(" sin 213 : {}" , sin(213));
println!(" sin 213 : {}" , TryInto::<f64>::try_into(sin(213)).unwrap());
println!(" Rust sin 213 : {}" , f64::sin(213.0 * FPI / 180.0));
println!(" sin 312 : {}" , sin(312));
println!(" sin 312 : {}" , TryInto::<f64>::try_into(sin(312)).unwrap());
println!(" Rust sin 312 : {}" , f64::sin(312.0 * FPI / 180.0));
println!(" sin 876 : {}" , sin(876));
println!(" sin 876 : {}" , TryInto::<f64>::try_into(sin(876)).unwrap());
println!(" Rust sin 876 : {}" , f64::sin(876.0 * FPI / 180.0));
println!(" cos 9 : {}" , cos(9));
println!(" cos 9 : {}" , TryInto::<f64>::try_into(cos(9)).unwrap());
println!(" Rust cos 9 : {}" , f64::cos(9.0 * FPI / 180.0));
println!(" cos 17 : {}" , cos(17));
println!(" cos 17 : {}" , TryInto::<f64>::try_into(cos(17)).unwrap());
println!(" Rust cos 17 : {}" , f64::cos(17.0 * FPI / 180.0));
println!(" cos 31 : {}" , cos(31));
println!(" cos 31 : {}" , TryInto::<f64>::try_into(cos(31)).unwrap());
println!(" Rust cos 31 : {}" , f64::cos(31.0 * FPI / 180.0));
println!(" cos 45 : {}" , cos(45));
println!(" cos 45 : {}" , TryInto::<f64>::try_into(cos(45)).unwrap());
println!(" Rust cos 45 : {}" , f64::cos(45.0 * FPI / 180.0));
println!(" cos 73 : {}" , cos(73));
println!(" cos 73 : {}" , TryInto::<f64>::try_into(cos(73)).unwrap());
println!(" Rust cos 73 : {}" , f64::cos(73.0 * FPI / 180.0));
println!(" cos 123 : {}" , cos(123));
println!(" cos 123 : {}" , TryInto::<f64>::try_into(cos(123)).unwrap());
println!(" Rust cos 123 : {}" , f64::cos(123.0 * FPI / 180.0));
println!(" cos 213 : {}" , cos(213));
println!(" cos 213 : {}" , TryInto::<f64>::try_into(cos(213)).unwrap());
println!(" Rust cos 213 : {}" , f64::cos(213.0 * FPI / 180.0));
println!(" cos 312 : {}" , cos(312));
println!(" cos 312 : {}" , TryInto::<f64>::try_into(cos(312)).unwrap());
println!(" Rust cos 312 : {}" , f64::cos(312.0 * FPI / 180.0));
println!(" cos 876 : {}" , cos(876));
println!(" cos 876 : {}" , TryInto::<f64>::try_into(cos(876)).unwrap());
println!(" Rust cos 876 : {}" , f64::cos(876.0 * FPI / 180.0));
}