diff --git a/Gemfile b/Gemfile index 59df67b..27677dc 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'http://rubygems.org' -ruby '2.6.6' +ruby '2.6.5' gem 'rake' diff --git a/lib/binary_to_decimal.rb b/lib/binary_to_decimal.rb index 439e8c6..6e056eb 100644 --- a/lib/binary_to_decimal.rb +++ b/lib/binary_to_decimal.rb @@ -5,5 +5,77 @@ # Calculate and return the decimal value for this binary number using # the algorithm you devised in class. def binary_to_decimal(binary_array) - raise NotImplementedError + decimal = 0 + index = 0 + + until index == binary_array.length + power = (binary_array.length - 1) - index + + decimal += binary_array[index] * ( 2 ** (power)) + + index += 1 + end + + return decimal end + + +# Method named decimal_to_binary receives decimal number as input +# Calculates and returns the binary value as a number for the decimal number +def decimal_to_binary(decimal) + binary = 0 + power = 0 + + if decimal.class == Integer + until decimal == 0 + binary_digit = decimal % 2 + binary += binary_digit * (10 ** power) + + decimal /= 2 + power += 1 + end + + return binary + elsif decimal.class == Float + decimal_before_dot = decimal - (decimal % 1) + binary_before_dot = 0 + + + until decimal_before_dot == 0 + binary_digit = decimal_before_dot % 2 + binary_before_dot += binary_digit * (10 ** power) + + + decimal_before_dot = (decimal_before_dot / 2) + decimal_before_dot = decimal_before_dot - (decimal_before_dot % 1) + + power += 1 + end + + + decimal_after_dot = (decimal % 1) + binary_after_dot_string = "0." + + until decimal_after_dot == 0 + binary_digit = (decimal_after_dot * 2) - ((decimal_after_dot * 2) % 1) + + if binary_digit == 0 + binary_after_dot_string << "0" + elsif binary_digit == 1 + binary_after_dot_string << "1" + end + + decimal_after_dot = (decimal_after_dot * 2) % 1 + end + + binary_after_dot = binary_after_dot_string.to_f + + + binary = binary_before_dot + binary_after_dot + + return binary + else # if decimal input is not an integer or float + raise ArgumentError, "Not a number." + end + +end \ No newline at end of file diff --git a/test/binary_to_decimal_test.rb b/test/binary_to_decimal_test.rb index 18d4bb3..7bb6f4c 100644 --- a/test/binary_to_decimal_test.rb +++ b/test/binary_to_decimal_test.rb @@ -33,3 +33,33 @@ expect(binary_to_decimal(binary_array)).must_equal expected_decimal end end + + +describe "decimal to binary" do + it "From 0 to 0" do + decimal = 0 + expected_binary = 0 + + expect(decimal_to_binary(decimal)).must_equal expected_binary + end + + it "From 87 to 1010111" do + decimal = 87 + expected_binary = 1010111 + + expect(decimal_to_binary(decimal)).must_equal expected_binary + end + + it "From 3.703125 to 11.101101" do + decimal = 3.703125 + expected_binary = 11.101101 + + expect(decimal_to_binary(decimal)).must_equal expected_binary + end + + it "Raises an ArgumentError if decimal is not a number" do + decimal = 'a' + + expect{ decimal_to_binary(decimal) }.must_raise ArgumentError + end +end