abs max feature implementation

This commit is contained in:
Sahil Afrid Farookhi 2021-05-31 21:56:36 +05:30
parent c923b270c3
commit a007c2b3e2
2 changed files with 31 additions and 0 deletions

View file

@ -66,6 +66,7 @@
* [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb) * [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb)
## Maths ## Maths
* [Abs Max](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_max.rb)
* [Abs](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs.rb) * [Abs](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs.rb)
* [Abs Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_test.rb) * [Abs Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_test.rb)
* [Add](https://github.com/TheAlgorithms/Ruby/blob/master/maths/add.rb) * [Add](https://github.com/TheAlgorithms/Ruby/blob/master/maths/add.rb)

30
maths/abs_max.rb Normal file
View file

@ -0,0 +1,30 @@
# 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
max_value = ((x + y + num.abs()) / 2)
"The Abs Max of #{x} and #{y} is #{max_value}."
rescue
"Error: Provide number only!"
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
puts abs_max(2, "-1")
# Error: Provide number only!
puts abs_max("3", "5")
# Error: Provide number only!
puts abs_max("a", "5")
# Error: Provide number only!