how to perform modulo (%), power (**), and equality (== or <=>); : Rational Numbers « Number « Ruby






how to perform modulo (%), power (**), and equality (== or <=>);



require 'rational'
require 'mathn'

rat = Rational(25/100) # => 1/4 -- lowest terms

rat % Rational(1/2) # => 1/4 -- modulo or remainder

rat**2 # => 1/16 -- exponent or power

rat == 1/8 # => false -- equality
rat == 1/4 # => true
rat <=> 1/4 # => 0
rat <=> 1/8 # => 1
rat <=> 1/2 # => -1

 








Related examples in the same category

1.Rational Numbers
2.how to create a fraction;
3.Create a rational number
4.Add two rational number up
5.Subtract one number from another
6.Rational number without using Rational class
7.Multiply two rational numbers
8.Divide two rational numbers
9.how to add (+), subtract (-), multiply (*), and divide (/) fractions;
10.how to produce a string or a float representation of a fraction (inspect).
11.Compare two rational numbers
12.Comparable operator on rational numbers
13.Inspect a rational number
14.Power a rational number
15.Modulo two rational numbers