TheAlgorithms-Ruby/maths/abs_min.rb

35 lines
671 B
Ruby
Raw Permalink Normal View History

2021-06-02 08:49:09 +02:00
# A ruby program to find absolute minimum
# Mathematical representation of abs min = ((a + b - absoulte(a - b)) / 2)
def abs_min(x, y)
num = x - y
2021-09-03 22:24:58 +02:00
min_value = ((x + y - num.abs) / 2)
2021-06-02 08:49:09 +02:00
"The Abs Min of #{x} and #{y} is #{min_value}."
2021-09-03 22:24:58 +02:00
rescue StandardError
'Error: Provide number only!'
2021-06-02 08:49:09 +02:00
end
#
# Valid inputs
#
puts abs_min(10, 20)
# The Abs Min of 10 and 20 is 10.
puts abs_min(-10, -1)
# The Abs Min of -10 and -1 is -10.
puts abs_min(9, -121)
# The Abs Min of 9 and -121 is -121.
#
# Invalid inputs
#
2021-09-03 22:24:58 +02:00
puts abs_min(2, '-1')
2021-06-02 08:49:09 +02:00
# Error: Provide number only!
2021-09-03 22:24:58 +02:00
puts abs_min('3', '5')
2021-06-02 08:49:09 +02:00
# Error: Provide number only!
2021-09-03 22:24:58 +02:00
puts abs_min('a', '5')
2021-06-02 08:49:09 +02:00
# Error: Provide number only!