Ruby/Number/Rational Numbers

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Add two rational number up

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat + 1/4 # => 1/2



Comparable operator on rational numbers

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
rat <=> 1/4 # => 0
rat <=> 1/8 # => 1
rat <=> 1/2 # => -1



Compare two rational numbers

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat == 1/8 # => false -- equality
rat == 1/4 # => true



Create a rational number

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms



Divide two rational numbers

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat / 2 # => 1/8 -- divide



how to add (+), subtract (-), multiply (*), and divide (/) fractions;

require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
rat + Rational(1/4) # =>  1/2 -- add
rat + 1/4 # => 1/2
rat - Rational(1/8) # => 1/8 -- subtract
rat - 1/8 # => 1/8
rat * 3 # => 3/4 -- multiply
rat / 2 # => 1/8 -- divide



how to create a fraction;

require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms



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



how to produce a string or a float representation of a fraction (inspect).

require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat.inspect # => "1/4"
rat.to_s # => "1/4"
rat.to_f # => 0.25
p rat # => 1/4



Inspect a rational number

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat.inspect # => "1/4"



Modulo two rational numbers

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat % Rational(1/2) # => 1/4 -- modulo or remainder



Multiply two rational numbers

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat * 3 # => 3/4 -- multiply



Power a rational number

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat**2 # => 1/16 -- exponent or power



Rational Numbers

A rational number is a number that can be expressed as a fraction of integers. 
Ruby supports the use of rational numbers via the Rational class. 
To use the Rational class, you must require it in the program. 
You create a rational number with the Rational method. 
Rational expects you to use integers, by the way. 
It will generate errors if you use floats.



Rational number without using Rational class

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat - 1/8 # => 1/8



Subtract one number from another

#!/usr/bin/env ruby
require "rational"
require "mathn"
rat = Rational(25/100) # => 1/4 -- lowest terms
 
rat - Rational(1/8) # => 1/8 -- subtract