Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. use std::convert::{ Into, From };
  2.  
  3. enum Integer { I8(i8), I16(i16), I32(i32), U8(u8), U16(u16), U32(u32) }
  4.  
  5. enum Float { F32(f32), F64(f64) }
  6.  
  7. enum Number { F(Float), I(Integer) }
  8.  
  9.  
  10. impl Into<f32> for Integer {
  11. fn into(self) -> f32 {
  12. match self {
  13. Integer::I8(n) => return n as f32,
  14. Integer::I16(n) => return n as f32,
  15. Integer::I32(n) => return n as f32,
  16. Integer::U8(n) => return n as f32,
  17. Integer::U16(n) => return n as f32,
  18. Integer::U32(n) => return n as f32,
  19. }
  20. }
  21. }
  22.  
  23. impl Into<f32> for Float {
  24. fn into(self) -> f32 {
  25. match self {
  26. Float::F32(n) => return n,
  27. Float::F64(n) => return n as f32
  28. }
  29. }
  30. }
  31.  
  32. impl Into<f32> for Number {
  33. fn into(self) -> f32 {
  34. match self {
  35. Number::F(n) => return n.into(),
  36. Number::I(n) => return n.into()
  37. }
  38. }
  39. }
  40.  
  41. fn sum<T>(a: T, b: T) -> f32 where T: Into<f32> {
  42. return a.into() + b.into();
  43. }
  44.  
  45. fn multiply<T>(a: T, b: T) -> f32 where T: Into<f32> {
  46. return a.into() * b.into();
  47. }
  48.  
  49. fn subtract<T>(a: T, b: T) -> f32 where T: Into<f32> {
  50. return a.into() - b.into();
  51. }
  52.  
  53. fn divide<T>(a: T, b: T) -> f32 where T: Into<f32> {
  54. return a.into() / b.into();
  55. }
  56.  
  57. fn pow<T>(base: T, expo: T) -> f32 where T: Into<f32> {
  58. return base.into().powf(expo.into());
  59. }
  60.  
  61. fn sqrt(value: Number) -> f32 {
  62. return pow(value, Number::F(Float::F32(0.5)));
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement