diff --git a/src/main.rs b/src/main.rs index b5302a7..9f06596 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,11 +110,11 @@ impl RoyaltyTree { self.tree[index].lazy=FiNum::zero(); } else { let lazy=self.tree[index].lazy; - let d1=lazy*self.weight_here(index)/self.weight_here_below(index); + let d1=if lazy>0.into() { lazy*self.weight_here(index)/self.weight_here_below(index) } else { FiNum::zero() }; let d02=lazy-d1; let index_left =wt_left (index).unwrap(); let index_right=wt_right(index).unwrap(); - let d0=d02*self.weight_here_below(index_left)/self.weight_below(index); + let d0=if d02>0.into() { d02*self.weight_here_below(index_left)/self.weight_below(index) } else { FiNum::zero() }; let d2=d02-d0; assert!(lazy==d0+d1+d2,"Distributing amounts that don't add up."); self.tree[index_left ].lazy+=d0; @@ -140,19 +140,20 @@ impl RoyaltyTree { } fn add_weight(&mut self, index: usize, weight: FiNum) { println!("Add_weight at {}",index); - if self.tree.len()>0 { + if self.tree.len()>0 { let mut ff0=self.forefather(); - self.expand_to(wt_forefather(index)*2); - let ff=self.forefather(); - while ff0!=ff { - let w=self.tree[ff0].weight; - ff0=wt_parent(ff0); - self.tree[ff0].weight=w; - } + let ff1=wt_forefather(index); + let w=self.tree[ff0].weight; + self.expand_to(ff1*2); + while ff0