2012年6月22日金曜日

開発環境

『Learning Ruby』(Michael Fitzgerald 著、O'Reilly Media、2007年、ISBN978-0-596-52986-4)の Chapter 5(Math)Review Questions 5を解いてみる。

その他参考書籍

5.

Fractional numbers

有理数(分数)のいろいろなメソッドを使ってみる。

コード(TextWrangler)

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'rational'

a = Rational(1,10)
b = Rational(1,100)
puts "#{a} + #{b} = #{a + b}"
c = 5
d = 10
puts "#{c}/#{d} = #{c.quo d}"
puts "#{a}: 分子 #{a.numerator} 分母 #{a.denominator}"
puts "#{b} = #{b.to_f}"

入出力結果(Terminal)

$ ./sample.rb
1/10 + 1/100 = 11/100
5/10 = 1/2
1/10: 分子 1 分母 10
1/100 = 0.01
$

0 コメント:

コメントを投稿