Browse Source

Fix reduce code

master
Georg Hopp 6 years ago
parent
commit
8108c69c55
Signed by: ghopp GPG Key ID: 4C5D226768784538
  1. 6
      fractional/src/fractional.rs
  2. 61
      fractional/src/main.rs

6
fractional/src/fractional.rs

@ -61,7 +61,11 @@ impl Fractional {
// the precision but ensures smaller numbers for numerator and // the precision but ensures smaller numbers for numerator and
// denominator. // denominator.
if _d > 1 && (_n % _d) * 10000000 < _n { if _d > 1 && (_n % _d) * 10000000 < _n {
Self(_n / _d, 1)
if n == _n {
Self(_n / _d, 1)
} else {
Self(1, _n / _d)
}
} else { } else {
Self(n / hcf(n, d), d / hcf(n, d)) Self(n / hcf(n, d), d / hcf(n, d))
} }

61
fractional/src/main.rs

@ -21,6 +21,8 @@
use std::convert::{TryFrom, TryInto, Into}; use std::convert::{TryFrom, TryInto, Into};
use std::num::TryFromIntError; use std::num::TryFromIntError;
use std::f64::consts::PI as FPI; use std::f64::consts::PI as FPI;
use std::fmt::Display;
use std::ops::{Add,Sub,Neg,Mul,Div};
use fractional::continuous::Continuous; use fractional::continuous::Continuous;
use fractional::fractional::{Fractional, from_vector}; use fractional::fractional::{Fractional, from_vector};
@ -96,7 +98,7 @@ fn pi() {
} }
fn _sin() { fn _sin() {
for d in [ 0, 45, 90, 135, 180, 225, 270, 315
for d in [ 0, 30, 45, 90, 135, 180, 225, 270, 315
, 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() { , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
let s = Fractional::sin(*d as i32); let s = Fractional::sin(*d as i32);
let sr :f64 = s.try_into().unwrap(); let sr :f64 = s.try_into().unwrap();
@ -107,7 +109,7 @@ fn _sin() {
} }
fn _tan() { fn _tan() {
for d in [ 0, 45, 90, 135, 180, 225, 270, 315
for d in [ 0, 30, 45, 90, 135, 180, 225, 270, 315
, 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() { , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
let t = Fractional::tan(*d as i32); let t = Fractional::tan(*d as i32);
let tr :f64 = t.try_into().unwrap(); let tr :f64 = t.try_into().unwrap();
@ -118,7 +120,7 @@ fn _tan() {
} }
fn _cos() { fn _cos() {
for d in [ 0, 45, 90, 135, 180, 225, 270, 315
for d in [ 0, 30, 45, 90, 135, 180, 225, 270, 315
, 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() { , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
let c = Fractional::cos(*d as i32); let c = Fractional::cos(*d as i32);
let cr :f64 = c.try_into().unwrap(); let cr :f64 = c.try_into().unwrap();
@ -128,11 +130,25 @@ fn _cos() {
} }
} }
fn _vector() {
fn _vector1() {
let v1 = Vector(1.into(), 2.into(), 3.into()); let v1 = Vector(1.into(), 2.into(), 3.into());
let v2 = Vector(2.into(), 2.into(), 3.into()); let v2 = Vector(2.into(), 2.into(), 3.into());
let s :Fractional = 3.into(); let s :Fractional = 3.into();
_vector(v1, v2, s);
}
fn _vector2() {
let v1 = Vector(1.0, 2.0, 3.0);
let v2 = Vector(2.0, 2.0, 3.0);
let s = 3.0;
_vector(v1, v2, s);
}
fn _vector<T>(v1 :Vector<T>, v2 :Vector<T>, s :T)
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T> + Trig + Copy + Display {
println!("{:>14} : {}", "Vector v1", v1); println!("{:>14} : {}", "Vector v1", v1);
println!("{:>14} : {}", "Vector v2", v2); println!("{:>14} : {}", "Vector v2", v2);
println!("{:>14} : {}", "abs v1", v1.abs()); println!("{:>14} : {}", "abs v1", v1.abs());
@ -153,16 +169,34 @@ fn _vector() {
println!("{:>14} : {}", "v2 * v1", v2 * v1); println!("{:>14} : {}", "v2 * v1", v2 * v1);
} }
fn _transform() {
let v = Vector(Fractional(1,1), Fractional(1,1), Fractional(1,1));
let v1 = Vector(Fractional(1,1), Fractional(2,1), Fractional(3,1));
let mt = translate(v);
fn _transform1() {
let v = Vector(Fractional(1,1), Fractional(1,1), Fractional(1,1));
let v1 = Vector(Fractional(1,1), Fractional(2,1), Fractional(3,1));
let v2 = Vector(Fractional(1,1), Fractional(1,1), Fractional(0,1));
let v3 = Vector(Fractional(1,1), Fractional(0,1), Fractional(1,1));
_transform(v, v1, v2, v3);
}
fn _transform2() {
let v = Vector(1.0, 1.0, 1.0);
let v1 = Vector(1.0, 2.0, 3.0);
let v2 = Vector(1.0, 1.0, 0.0);
let v3 = Vector(1.0, 0.0, 1.0);
_transform(v, v1, v2, v3);
}
fn _transform<T>(v :Vector<T>, v1 :Vector<T>, v2 :Vector<T>, v3 :Vector<T>)
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T> + Trig
+ From<i32> + Copy + Display {
let mt = translate(v);
println!("{:>14} : {}", "Vector v1", v1); println!("{:>14} : {}", "Vector v1", v1);
println!("{:>14} : {}", "translate v1", mt.apply(&v1)); println!("{:>14} : {}", "translate v1", mt.apply(&v1));
println!(); println!();
let v2 = Vector(1.into(), 1.into(), 0.into());
println!("{:>14} : {}", "Vector v2", v2); println!("{:>14} : {}", "Vector v2", v2);
for d in [ 30, 45, 60, 90, 120, 135, 150, 180 for d in [ 30, 45, 60, 90, 120, 135, 150, 180
, 210, 225, 240, 270, 300, 315, 330 ].iter() { , 210, 225, 240, 270, 300, 315, 330 ].iter() {
@ -186,7 +220,6 @@ fn _transform() {
} }
println!(); println!();
let v3 = Vector(Fractional(1,1), Fractional(0,1), Fractional(1,1));
println!("{:>14} : {}", "Vector v3", v3); println!("{:>14} : {}", "Vector v3", v3);
for d in [ 30, 45, 60, 90, 120, 135, 150, 180 for d in [ 30, 45, 60, 90, 120, 135, 150, 180
, 210, 225, 240, 270, 300, 315, 330 ].iter() { , 210, 225, 240, 270, 300, 315, 330 ].iter() {
@ -217,7 +250,11 @@ fn main() {
println!(); println!();
_tan(); _tan();
println!(); println!();
_vector();
_vector1();
println!();
_vector2();
println!();
_transform1();
println!(); println!();
_transform();
_transform2();
} }
Loading…
Cancel
Save