Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 3.65 KB | None | 0 0
  1. function cast_vectors(T)
  2.     x = [T(2.718281828), T(-3.141592654), T(1.414213562), T(0.5772156649), T(0.3010299957)]
  3.     y = [T(1486.2497), T(878366.9879), T(-22.37492), T(4773714.647), T(0.000185049)]
  4.     return x, y
  5. end
  6.  
  7. function split_sign(x)
  8.     nonnegative = []
  9.     negative = []
  10.     for i = 1 : length(x)
  11.         if x[i] < 0
  12.             push!(negative, x[i])
  13.         else
  14.             push!(nonnegative, x[i])
  15.         end
  16.     end
  17.     return nonnegative, negative
  18. end
  19.  
  20. function approximation_error(x, y)
  21.     return Float64(abs(x - y)) / Float64(abs(y))
  22. end
  23.  
  24. function algorithm1(x, y, T)
  25.     S = T(0.0)
  26.     for i = 1 : length(x)
  27.         S = S + T(x[i]) * T(y[i])
  28.     end
  29.     return S
  30. end
  31.  
  32. function algorithm2(x, y, T)
  33.     S = T(0.0)
  34.     for i = length(x) : - 1 : 1
  35.         S = S + T(x[i]) * T(y[i])
  36.     end
  37.     return S
  38. end
  39.  
  40. function algorithm3(x, y, T)
  41.     xy = []
  42.     for i = 1 : length(x)
  43.         push!(xy, T(x[i]) * T(y[i]))
  44.     end
  45.     xy_nonnegative, xy_negative = split_sign(xy)
  46.     sort(xy_nonnegative, rev = true) #sorting nonnegative numbers in reversed order (from largest to smallest)
  47.     sort(xy_negative) #sorting negative numbers (from smallest to largest)
  48.  
  49.     S_plus = T(0.0)
  50.     for i = 1 : length(xy_nonnegative)
  51.         S_plus = S_plus + T(xy_nonnegative[i])
  52.     end
  53.  
  54.     S_minus = T(0.0)
  55.     for i = 1 : length(xy_negative)
  56.         S_minus = S_minus + T(xy_negative[i])
  57.     end
  58.  
  59.     S = S_plus + S_minus
  60.     return S
  61. end
  62.  
  63. function algorithm4(x, y, T)
  64.     xy = []
  65.     for i = 1 : length(x)
  66.         push!(xy, T(x[i]) * T(y[i]))
  67.     end
  68.     xy_nonnegative, xy_negative = split_sign(xy)
  69.     sort(xy_nonnegative) #sorting nonnegative numbers (from smallest to largest)
  70.     sort(xy_negative, rev = true) #sorting negative numbers in reversed order (from largest to smallest)
  71.  
  72.     S_plus = T(0.0)
  73.     for i = 1 : length(xy_nonnegative)
  74.         S_plus = S_plus + T(xy_nonnegative[i])
  75.     end
  76.  
  77.     S_minus = T(0.0)
  78.     for i = 1 : length(xy_negative)
  79.         S_minus = S_minus + T(xy_negative[i])
  80.     end
  81.  
  82.     S = S_plus + S_minus
  83.     return S
  84. end
  85.  
  86. value = Float64(-1.00657107000000e-11)
  87.  
  88. x, y = cast_vectors(Float32)
  89. algorithm1_value = algorithm1(x, y, Float32)
  90. algorithm2_value = algorithm2(x, y, Float32)
  91. algorithm3_value = algorithm3(x, y, Float32)
  92. algorithm4_value = algorithm4(x, y, Float32)
  93. println("algorithm1(x, y, Float32):\t", algorithm1_value, ",\t\t\tapproximation error: \t", approximation_error(algorithm1_value, value))
  94. println("algorithm2(x, y, Float32):\t", algorithm2_value, ",\t\t\tapproximation error: \t", approximation_error(algorithm2_value, value))
  95. println("algorithm3(x, y, Float32):\t", algorithm3_value, ",\t\t\t\tapproximation error: \t", approximation_error(algorithm3_value, value))
  96. println("algorithm4(x, y, Float32):\t", algorithm4_value, ",\t\t\t\tapproximation error: \t", approximation_error(algorithm4_value, value))
  97.  
  98. x, y = cast_vectors(Float64)
  99. algorithm1_value = algorithm1(x, y, Float64)
  100. algorithm2_value = algorithm2(x, y, Float64)
  101. algorithm3_value = algorithm3(x, y, Float64)
  102. algorithm4_value = algorithm4(x, y, Float64)
  103. println("algorithm1(x, y, Float64):\t", algorithm1_value, ",\t\tapproximation error: \t", approximation_error(algorithm1_value, value))
  104. println("algorithm2(x, y, Float64):\t", algorithm2_value, ",\tapproximation error: \t", approximation_error(algorithm2_value, value))
  105. println("algorithm3(x, y, Float64):\t", algorithm3_value, ",\t\t\t\tapproximation error: \t", approximation_error(algorithm3_value, value))
  106. println("algorithm4(x, y, Float64):\t", algorithm4_value, ",\t\t\t\tapproximation error: \t", approximation_error(algorithm4_value, value))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement