Skip to content

Instantly share code, notes, and snippets.

@Houdini
Created February 4, 2011 21:47
Show Gist options
  • Save Houdini/811845 to your computer and use it in GitHub Desktop.
Save Houdini/811845 to your computer and use it in GitHub Desktop.
Это тест разных видов округллений и их комбинаций при валютном обмене
def round_math a
(a*100).round.to_f/100
end
def ceil_math a
(a*100).ceil.to_f/100
end
def floor_math a
(1*100).floor.to_f/100
end
def forward a
round_math a
# ceil_math a
# floor_math a
end
def back a
round_math a
# ceil_math a
# floor_math a
end
wrong, total = 0, 0
for i in (100...300)
for j in (1...31)
x = i.to_f + ((rand*100).round.to_f)/100
f = round_math 1 - j.to_f/100
delta_x = back(forward(x/f)*f)
if x != delta_x
p '--------------------------------'
p "x = #{x}; f = #{f}"
p "round(x/f) = #{forward(x/f)}"
p "round(round(x/f)*f) = #{delta_x}"
p '--------------------------------'
wrong += 1
end
total += 1
end
end
p "total = #{total}"
p "wrong = #{wrong}"
p "error = #{wrong.to_f/total}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment