Ruby/Rails syntax
  • Index
  • chap 1
  • chap 2
  • chap 3
  • chap 4
  • Enterprise Rails - big picture
  • Nokogiri
  • ActiveRecord - 進階功能
  • pack & unpack
  • performance
  • rails engine
  • jsonb / json / hstore
  • Deploy
  • Polymorphism/Polymorphic Associations
  • relationship
  • rvm / ENV
  • Auth
  • DB related
  • TODO N+1
  • SQL view
  • module
  • api + create-react-app
  • ONE_LINE
  • Delete & destroy association
Powered by GitBook
On this page
  • Number
  • Float vs BigDecimal (precision)
  • Fixnum vs Bignum

Was this helpful?

chap 3

Number

Numeric
  |-Integer
      |-Fixnum
      |-Bignum
  |-Float
  |-Complex
  |-Rational
  |-BigDecimal (Standard Library)
$ Fixnum.ancestors
=> [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]

$ Fixnum.included_modules
=> [Comparable, Kernel]

Type

ex

Fixnum

1

Bignum

111111111111

Float

Imprecise

5.0

BigDecimal

precise

3.0

Complex

imaginary numbers

(1+0i)

Rational

represent fractions

(2/3)

Float vs BigDecimal (precision)

0.2 + 0.1 == 0.3
# false

require 'bigdecimal'
BigDecimal("0.2") + BigDecimal("0.1") == 0.3
# true

BigDecimal is 12 times slower than Float

Fixnum vs Bignum

1.class
# Fixnum

100000000000.class
# Bignum
1.object_id
# 3
1.object_id # id is not changed
# 3 

9999999999999999999999999999999999.object_id
# 70225433369420
9999999999999999999999999999999999.object_id # id is changed
# 70225433617420

Fixnum like symbols in the interpreter level.

Bignum normal class & uses normal object ids.

Previouschap 2Nextchap 4

Last updated 5 years ago

Was this helpful?