TheAlgorithms-Ruby/maths/abs_max.rb

31 lines
659 B
Ruby
Raw Normal View History

2021-05-31 18:26:36 +02:00
# A ruby program to find absolute maximum
# Mathematical representation of abs max = ((a + b + absoulte(a - b)) / 2)
def abs_max(x, y)
num = x - y
2021-09-03 22:24:58 +02:00
max_value = ((x + y + num.abs) / 2)
2021-05-31 18:26:36 +02:00
"The Abs Max of #{x} and #{y} is #{max_value}."
2021-09-03 22:24:58 +02:00
rescue StandardError
'Error: Provide number only!'
2021-05-31 18:26:36 +02:00
end
# Valid inputs
puts abs_max(10, 20)
# The Abs Max of 10 and 20 is 20.
puts abs_max(-10, -1)
# The Abs Max of -10 and -1 is -1.
puts abs_max(9, -121)
# The Abs Max of 9 and -121 is 9.
# Invalid inputs
2021-09-03 22:24:58 +02:00
puts abs_max(2, '-1')
2021-05-31 18:26:36 +02:00
# Error: Provide number only!
2021-09-03 22:24:58 +02:00
puts abs_max('3', '5')
2021-05-31 18:26:36 +02:00
# Error: Provide number only!
2021-09-03 22:24:58 +02:00
puts abs_max('a', '5')
2021-05-31 18:26:36 +02:00
# Error: Provide number only!