Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Numbers & Range

 
Sree Va
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! Finally, a place to hold the count of all stars in the universe.


http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html

Numbers

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 -230 to 230-1 or -262 to 262-1) are held internally in binary form, and are objects of class Fixnum. Integers outside this range are stored in objects of class Bignum (currently implemented as a variable-length set of short integers). This process is transparent, and Ruby automatically manages the conversion back and forth.

num = 8
7.times do
print num.type, " ", num, "\n"
num *= num
end

produces:

Fixnum 8
Fixnum 64
Fixnum 4096
Fixnum 16777216
Bignum 281474976710656
Bignum 79228162514264337593543950336
Bignum 6277101735386680763835789423207666416102355444464034512896
 
Stuart Halloway
author
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruby is the first language I have used heavily where I don't even bother to remember what values fit in a Fixnum. The language deals elegantly enough with values that I can use my brain space for something else.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic