[前][次][番号順一覧][スレッド一覧]

ruby-changes:46177

From: stomar <ko1@a...>
Date: Sun, 9 Apr 2017 22:28:15 +0900 (JST)
Subject: [ruby-changes:46177] stomar:r58290 (trunk): numeric.c: improve docs for Float

stomar	2017-04-09 22:28:11 +0900 (Sun, 09 Apr 2017)

  New Revision: 58290

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58290

  Log:
    numeric.c: improve docs for Float
    
    * numeric.c: [DOC] mention possibly surprising behavior of
      Float#{floor,ceil,to_i,truncate} due to floating point arithmetic.

  Modified files:
    trunk/numeric.c
Index: numeric.c
===================================================================
--- numeric.c	(revision 58289)
+++ numeric.c	(revision 58290)
@@ -1964,6 +1964,11 @@ flo_prev_float(VALUE vx) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1964
  *     34567.89.floor(1)   #=> 34567.8
  *     34567.89.floor(2)   #=> 34567.89
  *     34567.89.floor(3)   #=> 34567.89
+ *
+ *  Note that the limited precision of floating point arithmetic
+ *  might lead to surprising results:
+ *
+ *     (0.3 / 0.1).floor  #=> 2 (!)
  */
 
 static VALUE
@@ -2026,6 +2031,11 @@ flo_floor(int argc, VALUE *argv, VALUE n https://github.com/ruby/ruby/blob/trunk/numeric.c#L2031
  *     34567.89.ceil(1)   #=> 34567.9
  *     34567.89.ceil(2)   #=> 34567.89
  *     34567.89.ceil(3)   #=> 34567.89
+ *
+ *  Note that the limited precision of floating point arithmetic
+ *  might lead to surprising results:
+ *
+ *     (2.1 / 0.7).ceil  #=> 4 (!)
  */
 
 static VALUE
@@ -2342,6 +2352,11 @@ float_invariant_round(double number, int https://github.com/ruby/ruby/blob/trunk/numeric.c#L2352
  *  Returns the +float+ truncated to an Integer.
  *
  *  Synonyms are #to_i and #to_int
+ *
+ *  Note that the limited precision of floating point arithmetic
+ *  might lead to surprising results:
+ *
+ *    (0.3 / 0.1).to_i  #=> 2 (!)
  */
 
 static VALUE
@@ -2377,6 +2392,11 @@ flo_to_i(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2392
  *     (-2.8).truncate        #=> -2
  *     1.234567.truncate(2)   #=> 1.23
  *     34567.89.truncate(-2)  #=> 34500
+ *
+ *  Note that the limited precision of floating point arithmetic
+ *  might lead to surprising results:
+ *
+ *     (0.3 / 0.1).truncate  #=> 2 (!)
  */
 static VALUE
 flo_truncate(int argc, VALUE *argv, VALUE num)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]