正文

Numbers of Standard Types in PR(2nd)2007-04-05 10:48:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/daweizhao/24608.html

分享到:

这是Programming Ruby(2nd)中的Charpter 5的第一个有关number的程序实例,如下: num=816.times do puts "#{num.class}:#{num}"num*=numend     结果如下: Fixnum:81Fixnum:6561Fixnum:43046721Bignum:1853020188851841Bignum:3433683820292512484657849089281Bignum:11790184577738583171520872861412518665678211592275841109096961 其中有Fixnum和Bignum,为什么?原因:机器指针可处理的定长整数. 大部分机器是31bit. 若运算结果超出Fixnum范围的话,自动将其变换为Bignum.之下是PR书上的一段原话:Ruby supports integers and floating-point numbers. Integers can be any length(up to a maximum determined by the amount of free memory on your system). Integers within a certain range (normally -2的30次方到2的30次方-1或者-2的62次方到2的62次方-1)are held internally in binary form and are objects of class Fixnum. Integers outside this range are stored in objets of class Bignum(currently implemented as a variable-length set of short integers). 自己又减去了一些东西,看看与原来的程序有何不同: num=816.times do puts "#{num}"  去掉了{num.class},即不显示数类名:Fixnum和Bignumnum*=numend 816561430467211853020188851841343368382029251248465784908928111790184577738583171520872861412518665678211592275841109096961 也可以写成: num=816.times do puts numnum*=numend 结果与上同 自总结:一个程序,可以去掉一些东西,看看与原来有什么不同。一定要理解通透,这样的话,能够旁枝末节地学到其它东西。      

阅读(1842) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册