際際滷

際際滷Share a Scribd company logo
The world without the
   literal notation for
   ?oating-point numbers


                         , Kenta Murata
   GeneticLab Co., Ltd.


Tuesday, July 19, 2011                    1
CRuby Committer
   (BigDecimal)

   Ruby Sapporo

   Kosen Conf


http://www.flickr.com/photos/koichiroo/5244581973/
Tuesday, July 19, 2011                               2
Tuesday, 1
2010 3 July 19, 2011   3
RubyKaigi
                        n   !!




Tuesday, 1
2010 3 July 19, 2011               4
Ruby    04
      Sapporo RubyKaigi 04

                         Summer 2012

Tuesday, July 19, 2011                  5
I have some question.




Tuesday, July 19, 2011                   6
Do you use Float
                             values?



Tuesday, July 19, 2011                      7
Do you understand
                     Float well?



Tuesday, July 19, 2011               8
Float is too dif?cult.




Tuesday, July 19, 2011                    9
Reading dif?culty



Tuesday, July 19, 2011                       10
ruby-1.9.2-p290 :001 > 76287755398823936.0
   => 76287755398823940.0
  ruby-1.9.2-p290 :002 >




Tuesday, July 19, 2011                         11
Comparing dif?culty



Tuesday, July 19, 2011            12
ruby-1.9.2-p290 :001 > 12.4 * 1.05 == 13.02
   => false


  ruby-1.9.2-p290 :002 > 12.4 * 1.05
   => 13.020000000000001
  ruby-1.9.2-p290 :003 >




Tuesday, July 19, 2011                          13
Representation
                           difference



Tuesday, July 19, 2011                    14
Human:	

	

 Decimal

                         Internal:	

 Binary


Tuesday, July 19, 2011                          15
Concept
                         difference



Tuesday, July 19, 2011                16
We think

                     Float is a number.


Tuesday, July 19, 2011                    17
It is real that

                         Float is a range.


Tuesday, July 19, 2011                       18
They¨re troublesome



Tuesday, July 19, 2011         19
There is a solution



Tuesday, July 19, 2011                   20
Why not use Rational.



Tuesday, July 19, 2011       21
It is dif?cult, too.



Tuesday, July 19, 2011                          22
Ruby doesn¨t have
            any literal notations
                for Rational


Tuesday, July 19, 2011              23
Does a fractional
                    notation solve the
                        problems?


Tuesday, July 19, 2011                   24
No!



Tuesday, July 19, 2011         25
We naturally use
   decimal point notation.



Tuesday, July 19, 2011       26
We need a decimal-
      point notation for
   representing a Rational.


Tuesday, July 19, 2011        27
ruby--p290 :001 > 13.02.class
   => Rational

  ruby--p290 :001 > 12.4 * 1.05 == 13.02
   => true

  ruby-1.9.2-p290 :002 > 12.4 * 1.05
   => 13.02

  ruby-1.9.2-p290 :003 > 13.02 / 3
   => (217/50)




Tuesday, July 19, 2011                     28
Do you worry about
              Rational is slow?



Tuesday, July 19, 2011            29
We can optimize for
            denominators of 10n




Tuesday, July 19, 2011            30
Float isn¨t as so fast as
           you think



Tuesday, July 19, 2011          31
If you need fast Float
              calculation



Tuesday, July 19, 2011           32
Write it in C as
                         extension library



Tuesday, July 19, 2011                       33
If you need to calculate
      with arrays of Float



Tuesday, July 19, 2011        34
Use NArray



Tuesday, July 19, 2011                35
Rational may be
                         what you want



Tuesday, July 19, 2011                     36
This talk¨s background,
    demonstrations, and
   more detail descriptions



Tuesday, July 19, 2011        37
Tuesday, July 19, 2011   38

More Related Content

The world without float literal