Guest User

Untitled

a guest
Jul 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. //
  2. // Sin functions
  3. //
  4. const S1: f64 = -0.166666666416265235595; /* -0x15555554cbac77.0p-55 */
  5. const S2: f64 = 0.0083333293858894631756; /* 0x111110896efbb2.0p-59 */
  6. const S3: f64 = -0.000198393348360966317347; /* -0x1a00f9e2cae774.0p-65 */
  7. const S4: f64 = 0.0000027183114939898219064; /* 0x16cd878c3b46a7.0p-71 */
  8.  
  9. pub fn k_sinf(x: f64) -> f32 {
  10. let z = x * x;
  11. let w = z * z;
  12. let r = S3 + z * S4;
  13. let s = z * x;
  14. ((x + s * (S1 + z * S2)) + s * w * r) as f32
  15. }
  16.  
  17. const S1_F32: f32 = -0.166666666416265235595; /* -0x15555554cbac77.0p-55 */
  18. const S2_F32: f32 = 0.0083333293858894631756; /* 0x111110896efbb2.0p-59 */
  19. const S3_F32: f32 = -0.000198393348360966317347; /* -0x1a00f9e2cae774.0p-65 */
  20. const S4_F32: f32 = 0.0000027183114939898219064; /* 0x16cd878c3b46a7.0p-71 */
  21.  
  22. fn k_sinf_f32(x: f32) -> f32 {
  23. let z = x * x;
  24. let w = z * z;
  25. let r = S3_F32 + z * S4_F32;
  26. let s = z * x;
  27. ((x + s * (S1_F32 + z * S2_F32)) + s * w * r)
  28. }
  29.  
  30. // Cos functinos
  31. const C0: f64 = -0.499999997251031003120; /* -0x1ffffffd0c5e81.0p-54 */
  32. const C1: f64 = 0.0416666233237390631894; /* 0x155553e1053a42.0p-57 */
  33. const C2: f64 = -0.00138867637746099294692; /* -0x16c087e80f1e27.0p-62 */
  34. const C3: f64 = 0.0000243904487962774090654; /* 0x199342e0ee5069.0p-68 */
  35.  
  36. pub fn k_cosf(x: f64) -> f32 {
  37. let z = x * x;
  38. let w = z * z;
  39. let r = C2 + z * C3;
  40. (((1.0 + z * C0) + w * C1) + (w * z) * r) as f32
  41. }
  42.  
  43. const C0_F32: f32 = -0.499999997251031003120; /* -0x1ffffffd0c5e81.0p-54 */
  44. const C1_F32: f32 = 0.0416666233237390631894; /* 0x155553e1053a42.0p-57 */
  45. const C2_F32: f32 = -0.00138867637746099294692; /* -0x16c087e80f1e27.0p-62 */
  46. const C3_F32: f32 = 0.0000243904487962774090654; /* 0x199342e0ee5069.0p-68 */
  47.  
  48. pub fn k_cosf_f32(x: f32) -> f32 {
  49. let z = x * x;
  50. let w = z * z;
  51. let r = C2_F32 + z * C3_F32;
  52. (((1.0 + z * C0_F32) + w * C1_F32) + (w * z) * r) as f32
  53. }
  54.  
  55. use std::f32::consts::PI;
  56.  
  57. fn main() {
  58. let mut sin_err = 0;
  59. let mut cos_err = 0;
  60.  
  61. for n in 0..=f32::to_bits(PI / 4.0) {
  62. let input = f32::from_bits(n);
  63.  
  64. sin_err = sin_err.max({
  65. let high_prec_sin = k_sinf(input as f64);
  66. let low_prec_sin = k_sinf_f32(input);
  67. (f32::to_bits(high_prec_sin) as i32)
  68. .wrapping_sub(f32::to_bits(low_prec_sin) as i32)
  69. .abs() as u32
  70. }).max({
  71. let high_prec_sin = k_sinf(-input as f64);
  72. let low_prec_sin = k_sinf_f32(-input);
  73. (f32::to_bits(high_prec_sin) as i32)
  74. .wrapping_sub(f32::to_bits(low_prec_sin) as i32)
  75. .abs() as u32
  76. });
  77.  
  78. cos_err = cos_err.max({
  79. let high_prec_cos = k_cosf(input as f64);
  80. let low_prec_cos = k_cosf_f32(input);
  81. (f32::to_bits(high_prec_cos) as i32)
  82. .wrapping_sub(f32::to_bits(low_prec_cos) as i32)
  83. .abs() as u32
  84. }).max({
  85. let high_prec_cos = k_cosf(-input as f64);
  86. let low_prec_cos = k_cosf_f32(-input);
  87. (f32::to_bits(high_prec_cos) as i32)
  88. .wrapping_sub(f32::to_bits(low_prec_cos) as i32)
  89. .abs() as u32
  90. });
  91. }
  92.  
  93. println!("Sin error (ULPs): {}", sin_err);
  94. println!("Cos error (ULPs): {}", cos_err);
  95. }
Add Comment
Please, Sign In to add comment