Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. fn lambda_sub(lam: f64) -> f64
  2. {
  3. //calculates the smallest power of 2 >= lambda
  4. let mut n = 1_f64;
  5. let mut m = 0_f64;
  6. if lam < 1_f64
  7. {
  8. while lam < n
  9. {
  10. n = n / 2_f64;
  11. m = m+1_f64;
  12. }
  13. return m
  14. }
  15. else if lam > 1_f64
  16. {
  17. while n < lam
  18. {
  19. n = n*2_f64;
  20. m=m+1_f64;
  21. }
  22. return m
  23. }
  24. else
  25. {
  26. return 0_f64;
  27. }
  28. }
  29.  
  30. fn clamp(x: f64 , b: f64) -> f64{
  31. //output B is x > B
  32. //ouput -B if x < -B
  33. //output x otherwise
  34. if x > b
  35. {
  36. return b;
  37. }
  38. else if x < -b
  39. {
  40. return -b;
  41. }
  42. else
  43. {
  44. return x;
  45. }
  46. }
  47.  
  48. fn round(l_sub: f64, inner_clamp: f64) -> f64
  49. {
  50. //rounds the inner clamp function to closest multiple of
  51. //l_sub with ties resolved towards pos inf
  52. let mut ans = l_sub;
  53. if ans >= inner_clamp
  54. {
  55. return ans;
  56. }
  57. else
  58. {
  59. while inner_clamp > ans
  60. {
  61. ans += l_sub;
  62. }
  63. if ans - inner_clamp <= inner_clamp - (ans - l_sub)
  64. {
  65. return ans;
  66. }
  67. else
  68. {
  69. return ans - l_sub;
  70. }
  71. }
  72. }
  73.  
  74. fn main() {
  75. // should round to 0, rounds to 5
  76. // also, does not seem to work for any negative numbers
  77. let r = round(5.0, 1.0);
  78. println!("rounding result: {}", r);
  79.  
  80. // perhaps should allow for negative B?
  81. let c = clamp(100.0, -40.0);
  82. println!("clamping_result: {}", c);
  83.  
  84. // give
  85. let l = lambda_sub(0.1);
  86. println!("lambda calculation result: {}", l);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement