From a007c2b3e230b1b5387c5d547cffb2a199c5b48e Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi Date: Mon, 31 May 2021 21:56:36 +0530 Subject: [PATCH] abs max feature implementation --- DIRECTORY.md | 1 + maths/abs_max.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 maths/abs_max.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index bff4ebc..e40d772 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -66,6 +66,7 @@ * [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb) ## 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 Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_test.rb) * [Add](https://github.com/TheAlgorithms/Ruby/blob/master/maths/add.rb) diff --git a/maths/abs_max.rb b/maths/abs_max.rb new file mode 100644 index 0000000..d3130d9 --- /dev/null +++ b/maths/abs_max.rb @@ -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!