Commit 8108c69c55cce0201e6ce231a450d509782e9ae5

Authored by Georg Hopp
1 parent 725ece9a

Fix reduce code

@@ -61,7 +61,11 @@ impl Fractional { @@ -61,7 +61,11 @@ impl Fractional {
61 // the precision but ensures smaller numbers for numerator and 61 // the precision but ensures smaller numbers for numerator and
62 // denominator. 62 // denominator.
63 if _d > 1 && (_n % _d) * 10000000 < _n { 63 if _d > 1 && (_n % _d) * 10000000 < _n {
64 - Self(_n / _d, 1) 64 + if n == _n {
  65 + Self(_n / _d, 1)
  66 + } else {
  67 + Self(1, _n / _d)
  68 + }
65 } else { 69 } else {
66 Self(n / hcf(n, d), d / hcf(n, d)) 70 Self(n / hcf(n, d), d / hcf(n, d))
67 } 71 }
@@ -21,6 +21,8 @@ @@ -21,6 +21,8 @@
21 use std::convert::{TryFrom, TryInto, Into}; 21 use std::convert::{TryFrom, TryInto, Into};
22 use std::num::TryFromIntError; 22 use std::num::TryFromIntError;
23 use std::f64::consts::PI as FPI; 23 use std::f64::consts::PI as FPI;
  24 +use std::fmt::Display;
  25 +use std::ops::{Add,Sub,Neg,Mul,Div};
24 26
25 use fractional::continuous::Continuous; 27 use fractional::continuous::Continuous;
26 use fractional::fractional::{Fractional, from_vector}; 28 use fractional::fractional::{Fractional, from_vector};
@@ -96,7 +98,7 @@ fn pi() { @@ -96,7 +98,7 @@ fn pi() {
96 } 98 }
97 99
98 fn _sin() { 100 fn _sin() {
99 - for d in [ 0, 45, 90, 135, 180, 225, 270, 315 101 + for d in [ 0, 30, 45, 90, 135, 180, 225, 270, 315
100 , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() { 102 , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
101 let s = Fractional::sin(*d as i32); 103 let s = Fractional::sin(*d as i32);
102 let sr :f64 = s.try_into().unwrap(); 104 let sr :f64 = s.try_into().unwrap();
@@ -107,7 +109,7 @@ fn _sin() { @@ -107,7 +109,7 @@ fn _sin() {
107 } 109 }
108 110
109 fn _tan() { 111 fn _tan() {
110 - for d in [ 0, 45, 90, 135, 180, 225, 270, 315 112 + for d in [ 0, 30, 45, 90, 135, 180, 225, 270, 315
111 , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() { 113 , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
112 let t = Fractional::tan(*d as i32); 114 let t = Fractional::tan(*d as i32);
113 let tr :f64 = t.try_into().unwrap(); 115 let tr :f64 = t.try_into().unwrap();
@@ -118,7 +120,7 @@ fn _tan() { @@ -118,7 +120,7 @@ fn _tan() {
118 } 120 }
119 121
120 fn _cos() { 122 fn _cos() {
121 - for d in [ 0, 45, 90, 135, 180, 225, 270, 315 123 + for d in [ 0, 30, 45, 90, 135, 180, 225, 270, 315
122 , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() { 124 , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
123 let c = Fractional::cos(*d as i32); 125 let c = Fractional::cos(*d as i32);
124 let cr :f64 = c.try_into().unwrap(); 126 let cr :f64 = c.try_into().unwrap();
@@ -128,11 +130,25 @@ fn _cos() { @@ -128,11 +130,25 @@ fn _cos() {
128 } 130 }
129 } 131 }
130 132
131 -fn _vector() { 133 +fn _vector1() {
132 let v1 = Vector(1.into(), 2.into(), 3.into()); 134 let v1 = Vector(1.into(), 2.into(), 3.into());
133 let v2 = Vector(2.into(), 2.into(), 3.into()); 135 let v2 = Vector(2.into(), 2.into(), 3.into());
134 let s :Fractional = 3.into(); 136 let s :Fractional = 3.into();
135 137
  138 + _vector(v1, v2, s);
  139 +}
  140 +
  141 +fn _vector2() {
  142 + let v1 = Vector(1.0, 2.0, 3.0);
  143 + let v2 = Vector(2.0, 2.0, 3.0);
  144 + let s = 3.0;
  145 +
  146 + _vector(v1, v2, s);
  147 +}
  148 +
  149 +fn _vector<T>(v1 :Vector<T>, v2 :Vector<T>, s :T)
  150 + where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
  151 + + Mul<Output = T> + Div<Output = T> + Trig + Copy + Display {
136 println!("{:>14} : {}", "Vector v1", v1); 152 println!("{:>14} : {}", "Vector v1", v1);
137 println!("{:>14} : {}", "Vector v2", v2); 153 println!("{:>14} : {}", "Vector v2", v2);
138 println!("{:>14} : {}", "abs v1", v1.abs()); 154 println!("{:>14} : {}", "abs v1", v1.abs());
@@ -153,16 +169,34 @@ fn _vector() { @@ -153,16 +169,34 @@ fn _vector() {
153 println!("{:>14} : {}", "v2 * v1", v2 * v1); 169 println!("{:>14} : {}", "v2 * v1", v2 * v1);
154 } 170 }
155 171
156 -fn _transform() {  
157 - let v = Vector(Fractional(1,1), Fractional(1,1), Fractional(1,1));  
158 - let v1 = Vector(Fractional(1,1), Fractional(2,1), Fractional(3,1));  
159 - let mt = translate(v); 172 +fn _transform1() {
  173 + let v = Vector(Fractional(1,1), Fractional(1,1), Fractional(1,1));
  174 + let v1 = Vector(Fractional(1,1), Fractional(2,1), Fractional(3,1));
  175 + let v2 = Vector(Fractional(1,1), Fractional(1,1), Fractional(0,1));
  176 + let v3 = Vector(Fractional(1,1), Fractional(0,1), Fractional(1,1));
  177 +
  178 + _transform(v, v1, v2, v3);
  179 +}
  180 +
  181 +fn _transform2() {
  182 + let v = Vector(1.0, 1.0, 1.0);
  183 + let v1 = Vector(1.0, 2.0, 3.0);
  184 + let v2 = Vector(1.0, 1.0, 0.0);
  185 + let v3 = Vector(1.0, 0.0, 1.0);
  186 +
  187 + _transform(v, v1, v2, v3);
  188 +}
  189 +
  190 +fn _transform<T>(v :Vector<T>, v1 :Vector<T>, v2 :Vector<T>, v3 :Vector<T>)
  191 + where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
  192 + + Mul<Output = T> + Div<Output = T> + Trig
  193 + + From<i32> + Copy + Display {
  194 + let mt = translate(v);
160 195
161 println!("{:>14} : {}", "Vector v1", v1); 196 println!("{:>14} : {}", "Vector v1", v1);
162 println!("{:>14} : {}", "translate v1", mt.apply(&v1)); 197 println!("{:>14} : {}", "translate v1", mt.apply(&v1));
163 println!(); 198 println!();
164 199
165 - let v2 = Vector(1.into(), 1.into(), 0.into());  
166 println!("{:>14} : {}", "Vector v2", v2); 200 println!("{:>14} : {}", "Vector v2", v2);
167 for d in [ 30, 45, 60, 90, 120, 135, 150, 180 201 for d in [ 30, 45, 60, 90, 120, 135, 150, 180
168 , 210, 225, 240, 270, 300, 315, 330 ].iter() { 202 , 210, 225, 240, 270, 300, 315, 330 ].iter() {
@@ -186,7 +220,6 @@ fn _transform() { @@ -186,7 +220,6 @@ fn _transform() {
186 } 220 }
187 println!(); 221 println!();
188 222
189 - let v3 = Vector(Fractional(1,1), Fractional(0,1), Fractional(1,1));  
190 println!("{:>14} : {}", "Vector v3", v3); 223 println!("{:>14} : {}", "Vector v3", v3);
191 for d in [ 30, 45, 60, 90, 120, 135, 150, 180 224 for d in [ 30, 45, 60, 90, 120, 135, 150, 180
192 , 210, 225, 240, 270, 300, 315, 330 ].iter() { 225 , 210, 225, 240, 270, 300, 315, 330 ].iter() {
@@ -217,7 +250,11 @@ fn main() { @@ -217,7 +250,11 @@ fn main() {
217 println!(); 250 println!();
218 _tan(); 251 _tan();
219 println!(); 252 println!();
220 - _vector(); 253 + _vector1();
  254 + println!();
  255 + _vector2();
  256 + println!();
  257 + _transform1();
221 println!(); 258 println!();
222 - _transform(); 259 + _transform2();
223 } 260 }
Please register or login to post a comment