《Rubu基础教程第五版》第十二章笔记 数值类

数值类的构成

Numeric(数值) 子类包括(Interger整数,Float浮点小数,Rational有理数,Complex复数)

其中Intetger的子类有(Fixnum普通整数,Bignum大整数)

>> n = 2 ** 10
=> 1024
>> n.class
=> Integer
>> n = 2 ** 1000
=> 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
>> n.class
=> Integer
>> 

测试了以后发现改变了,都是Integer的实例了

Rational对象用"Rational(分子,分母)"的形式定义

>> a = Rational(2, 5)
=> (2/5)
>> b = Rational(1, 3)
=> (1/3)
>> p [a, b]
[(2/5), (1/3)]
=> [(2/5), (1/3)]
>> c = a+ b
=> (11/15)
>> c.to_f
=> 0.7333333333333333
>> c.to_i
=> 0
>> [c.numerator, c.denominator]
=> [11, 15]
>> 

数值的字面量

123 表示十进制数

0123 表示八进制整数

0o123 表示八进制整数

0d123 表示十进制整数

0x123 表示十六进制整数

0b1111011 表示二进制整数

123.45 表示浮点数

1.23e4   浮点小数的指数表示法

1.23e-4  浮点小数的指数表示法

123r  表示有理数(123/1)

123.45 表示有理数(12345/100)

算数运算  这个跟Python差不多,+-*/%**加减乘除 取余 幂次方

irb(main):001:0> 5/2
=> 2
irb(main):002:0> 5/2.0
=> 2.5
irb(main):003:0> 

 这个比较特殊,整数除以整数还是整数,整数除以浮点数得到浮点数

除法

除了/与%还有一些调用方法的操作

整数除,div

irb(main):004:0> 5.div(2)
=> 2
irb(main):005:0> 5.div(2.2)
=> 2
irb(main):006:0> 

x.quo(y) 返回x除以y后的商,如果x、y都是整数,则返回Rational对象

irb(main):006:0> 5.quo(2)
=> (5/2)
irb(main):007:0> 5.quo(2.2)
=> 2.2727272727272725
irb(main):008:0> 

x.modulo(y)等价与%取余

irb(main):009:0> 5.modulo(4)
=> 1
irb(main):010:0> 3.modulo(1.1)
=> 0.7999999999999998
irb(main):011:0> 3.modulo(2)
=> 1
irb(main):012:0> 

x.divmod(y)

将x处于y后的商和余数作为数组返回

irb(main):013:0> 5.divmod(2)
=> [2, 1]
irb(main):014:0> 

x.remainder(y)

irb(main):014:0> 10.remainder(3.5)
=> 3.0
irb(main):015:0> 5.remainder(3.5)
=> 1.5
irb(main):016:0> 5.remainder(2)
=> 1
irb(main):017:0> 5.remainder(3.50)
=> 1.5
irb(main):018:0> 5.remainder(3.51)
=> 1.4900000000000002
irb(main):019:0> 5.remainder(3.5)

 测试了,不知道有啥用

Math模块

irb(main):021:0> Math.methods
=> [:acosh, :asinh, :atanh, :exp, :log, :log2, :log10, :cbrt, :frexp, :ldexp, :hypot, :erf, :erfc, :gamma, :lgamma, :sqrt, :atan2, :cos, :sin, :tan, :acos, :asin, :atan, :cosh, :sinh, :tanh, :<=>, :<=, :>=, :==, :===, :included_modules, :include?, :name, :ancestors, :attr, :attr_reader, :attr_writer, :attr_accessor, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :freeze, :inspect, :private_constant, :public_constant, :const_missing, :deprecate_constant, :include, :singleton_class?, :prepend, :module_exec, :module_eval, :class_eval, :remove_method, :<, :>, :undef_method, :class_exec, :method_defined?, :alias_method, :to_s, :private_class_method, :public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, :public_instance_method, :define_method, :autoload, :autoload?, :instance_method, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :instance_variable_get, :instance_variable_set, :instance_variables, :singleton_method, :method, :public_send, :define_singleton_method, :public_method, :extend, :to_enum, :enum_for, :=~, :!~, :eql?, :respond_to?, :object_id, :send, :display, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :yield_self, :then, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :equal?, :!, :__id__, :instance_exec, :!=, :instance_eval, :__send__]

 有这么一堆方法

数值类型转换

to_i 装换成整形, to_f转换成浮点型

round 四舍五入 (复数就是往整数方法挪动)

irb(main):037:0> 0.12.round(1)
=> 0.1
irb(main):038:0> 0.12.round
=> 0
irb(main):039:0> 123.3.round
=> 123
irb(main):040:0> 123.3.round(-2)
=> 100
irb(main):041:0> 

位运算

随机数

Random.rand返回0到1的数值,写的很简单。

计数

数字可以用来做循环来计数用

n.time {|i| ...}

3.upto(5){|i| ...}

3到5,首尾都取

5.downto(3){|i| ...}

5到3,  首尾都取

2.step(10, 3){|i| ...}

开始2,跳跃3,结束10

如果不对这个方法指定块,就会返回Enumerator对象。

irb(main):066:0> ary = 2.step(10).collect{|i| i * 2}
=> [4, 6, 8, 10, 12, 14, 16, 18, 20]
irb(main):067:0> 

 这个用了collect的方法

近似值误差,可以通过Rational类进行运算

irb(main):066:0> ary = 2.step(10).collect{|i| i * 2}
=> [4, 6, 8, 10, 12, 14, 16, 18, 20]
irb(main):067:0> a = 1/10 + 2/10
=> 0
irb(main):068:0> a = 1/10.0 + 2/10.0
=> 0.30000000000000004
irb(main):069:0> a = 1/10r + 2/10r
=> (3/10)
irb(main):071:0> b = 3/10r
=> (3/10)
irb(main):072:0> p a,b
(3/10)
(3/10)
=> [(3/10), (3/10)]
irb(main):073:0> a == b
=> true
irb(main):074:0> 

Comparable模块

Comparable模块中的各运算符都会使用<=>运算符的结果

a < b 时 -1 a == b时 0 a>b时 1

class Vector
  include Comparable
  attr_accessor :x, :y

  def initialize(x, y)
    @x, @y = x, y
  end

  def scalar
    Math.sqrt(x ** 2 + y ** 2)
  end
  

  def <=> (other)       # 定义 <=>方法,通过Comparable模块,就可以实现大于小于的实现
    scalar <=> other.scalar
  end

end

v1 = Vector.new(2,6)
v2 = Vector.new(4, -4)

p v1 <=> v2
p v1 < v2
p v1 > v2

这个用在比较大小还是非常方便的。

回家作业

1、表示温度的单位有摄氏、华氏两种。请定义将摄氏转换为华氏的方法cels2fahr。具体公式 华氏 = 摄氏 X 9 / 5 + 32

#! /usr/bin/env ruby

def cels2fahr(cels)
  case cels
  when Integer, Float
    fahr = cels * 9 /5 + 32
    fahr
  else
    raise "input error"
  end
end

res = cels2fahr("100")
p res

2 与1相反,定义华氏度转换为摄氏度,然后从1华氏度到100华氏度,请按照每隔1华氏度输出一次的方式,输出对应的摄氏温度

shijianzhong@shijianzhongdeMacBook-Pro exercises % cat e2.rb 
#! /usr/bin/env ruby

def fahr2cels(fahr)
  case fahr
  when Integer,Float
    cels = (fahr - 32) * 5 / 9.0
    p cels.round(2)
  else
    raise "input error"
  end
end

(1..100).each {|n| fahr2cels(n)}

3 请定义返回扔骰子的结果的dice方法

#! /usr/bin/env ruby

def dice
  Random.rand(1..6)
end

puts dice
puts dice

4请定义合计扔10次骰子的结果的dice10方法

#! /usr/bin/env ruby

def dice10
  sum = 0
  10.times do 
    res = Random.rand(1..6)
    sum += res
  end
  sum
end

puts dice10
puts dice10

5请定义确实num是否为质数的prime?(num)方法。

#! /usr/bin/env ruby

def prime?(num)
  n = 2
  signal = true
  (num-2).times do
    res = num % n
    case res
      when 0 then
        signal = false
        break p "#{num} is not prime"
      end
    n += 1
  end
  if signal then
    p "#{num} is prime"
  end 
end

prime?(17)

 随便写了一个,感觉比较丑

原文地址:https://www.cnblogs.com/sidianok/p/13039354.html