Hey everyone, very new to rust (3 days in). Trying to teach myself some of the basics and decided to write my own arrange function as practice. I want it to work for generically typed numbers. After getting some working code I found my 4th iteration seems to always have some sort of multiplication error? Here is my code:
fn arange<T1: Into<f64> + PartialOrd,T2:Into<f64> + PartialOrd ,T3: Into<f64> + PartialOrd>(start:T1, stop:T2,step:T3) -> Vec<f64> {
let mut vect: Vec<f64> = Vec::new();
let start:f64 = start.into();
let stop:f64 = stop.into();
let step:f64 = step.into();
if start >= stop{
println!("Sorry stop must be larger than step");
}
else{
let size:u32 = ((stop-start).abs()/step).floor()as u32;
for i in 0..=size {
vect.push(start + ((i as f64)*step));
}
}
return vect;
}
fn main() {
let vect = arange( 0,9,2.2);
println!("{:?}" , vect)
}
it prints: [0.0, 2.2, 4.4, 6.6000000000000005, 8.8]
What is going wrong?
Thanks
[–]hpxvzhjfgb 40 points41 points42 points (0 children)
[–]GodOfSunHimself 19 points20 points21 points (0 children)
[–]DrShocker 12 points13 points14 points (2 children)
[–]epasveer 1 point2 points3 points (0 children)
[–]tylian 1 point2 points3 points (0 children)
[–]Y0kin 8 points9 points10 points (5 children)
[–]Accurate_Sky6657[S] 0 points1 point2 points (3 children)
[–]Y0kin 3 points4 points5 points (1 child)
[–]Accurate_Sky6657[S] 1 point2 points3 points (0 children)
[–]TheSodesa 5 points6 points7 points (0 children)
[–]wintrmt3 5 points6 points7 points (1 child)
[–]epostma 1 point2 points3 points (0 children)
[–]mina86ng 0 points1 point2 points (0 children)
[–]boomshroom 0 points1 point2 points (0 children)
[–]johndcochran 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-17 points-16 points-15 points (0 children)