首页 > 其他 > 详细

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

时间:2020-06-03 19:35:05      阅读:29      评论:0      收藏:0      [点我收藏+]

 

数值类的构成

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

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!