Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn lambda_sub(lam: f64) -> f64
- {
- //calculates the smallest power of 2 >= lambda
- let mut n = 1_f64;
- let mut m = 0_f64;
- if lam < 1_f64
- {
- while lam < n
- {
- n = n / 2_f64;
- m = m+1_f64;
- }
- return m
- }
- else if lam > 1_f64
- {
- while n < lam
- {
- n = n*2_f64;
- m=m+1_f64;
- }
- return m
- }
- else
- {
- return 0_f64;
- }
- }
- fn clamp(x: f64 , b: f64) -> f64{
- //output B is x > B
- //ouput -B if x < -B
- //output x otherwise
- if x > b
- {
- return b;
- }
- else if x < -b
- {
- return -b;
- }
- else
- {
- return x;
- }
- }
- fn round(l_sub: f64, inner_clamp: f64) -> f64
- {
- //rounds the inner clamp function to closest multiple of
- //l_sub with ties resolved towards pos inf
- let mut ans = l_sub;
- if ans >= inner_clamp
- {
- return ans;
- }
- else
- {
- while inner_clamp > ans
- {
- ans += l_sub;
- }
- if ans - inner_clamp <= inner_clamp - (ans - l_sub)
- {
- return ans;
- }
- else
- {
- return ans - l_sub;
- }
- }
- }
- fn main() {
- // should round to 0, rounds to 5
- // also, does not seem to work for any negative numbers
- let r = round(5.0, 1.0);
- println!("rounding result: {}", r);
- // perhaps should allow for negative B?
- let c = clamp(100.0, -40.0);
- println!("clamping_result: {}", c);
- // give
- let l = lambda_sub(0.1);
- println!("lambda calculation result: {}", l);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement