[#40528] [Feature #2833] 絵文字エンコーディングの提案 — Kenta Murata <redmine@...>

Feature #2833: 絵文字エンコーディングの提案

32 messages 2010/03/02
[#40530] Re: [Feature #2833] 絵文字エンコーディングの提案 — Yukihiro Matsumoto <matz@...> 2010/03/02

まつもと ゆきひろです

[#40597] Re: [ruby-list:46898] 重複組合せは組込みにならないのでしょうか? — "KISHIMOTO, Makoto" <ksmakoto@...4u.or.jp>

きしもとです

17 messages 2010/03/12
[#40598] Re: [ruby-list:46898] 重複組合せは組込みにならないのでしょうか? — Yukihiro Matsumoto <matz@...> 2010/03/12

まつもと ゆきひろです

[#40601] Re: [ruby-list:46898] 重複組合せは組込みにならないのでしょうか? — Yusuke ENDOH <mame@...> 2010/03/12

遠藤です。

[#40608] Re: 組込みの重複順列・重複組合せ — "KISHIMOTO, Makoto" <ksmakoto@...4u.or.jp> 2010/03/13

> 同様に、repeated_permutation/combination のデフォルト引数にも反対

[#40610] Re: 組込みの重複順列・重複組合せ — Yukihiro Matsumoto <matz@...> 2010/03/13

まつもと ゆきひろです

[#40641] [Bug #2965] method `===' called on hidden T_STRING object (NotImplementedError) — Kenta Murata <redmine@...>

Bug #2965: method `===' called on hidden T_STRING object (NotImplementedError)

12 messages 2010/03/15

[#40649] [Feature #2968] 数値の正負を返すメソッド — Yui NARUSE <redmine@...>

Feature #2968: 数値の正負を返すメソッド

17 messages 2010/03/15

[#40650] [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — Yui NARUSE <redmine@...>

Feature #2969: String#to_f が -h.hhh±pd を解釈できるように

38 messages 2010/03/15
[#40728] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — Tadayoshi Funaba <tadf@...> 2010/03/22

質問ですが、この形式は入力だけでなく、なんらかの方法で出力でも利用でき

[#40732] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — "NARUSE, Yui" <naruse@...> 2010/03/22

成瀬です。

[#40736] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — Tadayoshi Funaba <tadf@...> 2010/03/23

> String#to_f は従来から指数表記を許していたので、

[#40738] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — "NARUSE, Yui" <naruse@...> 2010/03/23

成瀬です。

[#40745] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — Tadayoshi Funaba <tadf@...> 2010/03/24

> to_i がデフォルトで prefix を見ないのは、0377 のような、

[#40747] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — "NARUSE, Yui" <naruse@...> 2010/03/24

成瀬です。

[#40749] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — Tadayoshi Funaba <tadf@...> 2010/03/24

> 先のパッチの対象関数が ruby_strtod である通り、

[#40759] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — "NARUSE, Yui" <naruse@...> 2010/03/25

成瀬です。

[#40762] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — Tadayoshi Funaba <tadf@...> 2010/03/25

> strtod(3) の解釈対象に含まれていない 2 進や 8 進を否定することが、

[#40763] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — "NARUSE, Yui" <naruse@...> 2010/03/25

(2010/03/26 3:05), Tadayoshi Funaba wrote:

[#40764] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — Tadayoshi Funaba <tadf@...> 2010/03/25

> なぜ同じなのでしょう。

[#40782] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — "NARUSE, Yui" <naruse@...> 2010/03/26

(2010/03/26 4:02), Tadayoshi Funaba wrote:

[#40786] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — KOSAKI Motohiro <kosaki.motohiro@...> 2010/03/27

>> strtod(3) を参考にしたり、影響されたりすることは普通にあるとは思います

[#40788] Re: [Feature #2969] String#to_f が -h.hhh±pd を解釈できるように — "NARUSE, Yui" <naruse@...> 2010/03/27

(2010/03/27 18:19), KOSAKI Motohiro wrote:

[#40695] keiju, please check tickets assigned to you — Yusuke ENDOH <mame@...>

いしつかさん

15 messages 2010/03/18

[#40779] [Feature #3018] UNINITIALIZED_VAR() マクロの導入 — Motohiro KOSAKI <redmine@...>

Feature #3018: UNINITIALIZED_VAR() マクロの導入

12 messages 2010/03/26

[#40805] Improvement of Fiber switching cost with system dependent way — SASADA Koichi <ko1@...>

 ささだです.

10 messages 2010/03/28

[ruby-dev:40595] Time#round

From: Tanaka Akira <akr@...>
Date: 2010-03-11 06:09:45 UTC
List: ruby-dev #40595
前にちょっと書きましたが、Time#round を追加するのはどうでしょう。
これは、秒未満の時刻を丸めるメソッドです。

ちょうど [ruby-core:28602] にもありましたが、
Time オブジェクト内に誤差を含む値を入れてしまうことがあります。

まぁ、時刻に誤差があるのは測定のことも考えるとしかたがないと思うのですが、
誤差のある時刻を近似する値を表示することが難しいという問題があります。

strftime の %N は、小数点以下の秒数を表示するものなので、
仕様上、丸めるのは困難で、切り捨てにせざるを得ません。
丸めると、秒への繰り上がりが生じた時にどうするかという問題が生じます。

%N だけでなく、usec や nsec も同様な理由で切り捨てです。

このため、たとえば 0.9999999 秒を表示すると 0秒になって、
1秒を期待している人には、うまく動かないように見えることになります。

ここで、Time#round を使うと、小数点以下を四捨五入した
Time オブジェクトを得られます。

  % ./ruby -e '
  t = Time.utc(1999,12,31, 23,59,59,876543)
  p t, t.subsec
  t = t.round
  p t, t.subsec'
  1999-12-31 23:59:59 UTC
  (876543/1000000)
  2000-01-01 00:00:00 UTC
  0

また、Integer#round と同様に、省略可能な引数で桁数を指定することができます。

これにより、秒を小数点以下3桁で丸めて表示したければ、
strftime の前に round(3) を呼ぶことで実現できます。

  % ./ruby -e '
  t = Time.utc(1999,12,31, 23,59,59,876543)
  p t, t.subsec, t.strftime("%T.%3N")
  t = t.round(3)
  p t, t.subsec, t.strftime("%T.%3N")
  '
  1999-12-31 23:59:59 UTC
  (876543/1000000)
  "23:59:59.876"
  1999-12-31 23:59:59 UTC
  (877/1000)
  "23:59:59.877"

どうでしょう?

% svn diff --diff-cmd diff -x '-u -p'
Index: time.c
===================================================================
--- time.c	(revision 26871)
+++ time.c	(working copy)
@@ -2997,6 +2997,64 @@ rb_time_succ(VALUE time)

 #define time_succ rb_time_succ

+/*
+ * call-seq:
+ *   time.round([ndigits])   => new_time
+ *
+ * Rounds sub seconds to a given precision in decimal digits (0
digits by default).
+ * It returns a new time object.
+ * _ndigits_ should be zero or positive integer.
+ *
+ *     require 'time'
+ *
+ *     t = Time.utc(1999,12,31, 23,59,59)
+ *     p((t + 0.4).round.iso8601(3))    #=> "1999-12-31T23:59:59.000Z"
+ *     p((t + 0.49).round.iso8601(3))   #=> "1999-12-31T23:59:59.000Z"
+ *     p((t + 0.5).round.iso8601(3))    #=> "2000-01-01T00:00:00.000Z"
+ *     p((t + 1.4).round.iso8601(3))    #=> "2000-01-01T00:00:00.000Z"
+ *     p((t + 1.49).round.iso8601(3))   #=> "2000-01-01T00:00:00.000Z"
+ *     p((t + 1.5).round.iso8601(3))    #=> "2000-01-01T00:00:01.000Z"
+ *
+ *     t = Time.utc(1999,12,31, 23,59,59)
+ *     p (t + 0.123456789).round(4).iso8601(6)  #=>
"1999-12-31T23:59:59.123500Z"
+ */
+
+static VALUE
+time_round(int argc, VALUE *argv, VALUE time)
+{
+    VALUE ndigits, v, a, b, den;
+    long nd;
+    struct time_object *tobj;
+
+    rb_scan_args(argc, argv, "01", &ndigits);
+
+    if (NIL_P(ndigits))
+        ndigits = INT2FIX(0);
+    else
+        ndigits = rb_to_int(ndigits);
+
+    nd = NUM2LONG(ndigits);
+    if (nd < 0)
+	rb_raise(rb_eArgError, "negative ndigits given");
+
+    GetTimeval(time, tobj);
+    v = rb_time_unmagnify(tobj->timexv);
+
+    a = INT2FIX(1);
+    b = INT2FIX(10);
+    while (0 < nd) {
+        if (nd & 1)
+            a = mul(a, b);
+        b = mul(b, b);
+        nd = nd >> 1;
+    }
+    den = quo(INT2FIX(1), a);
+    v = mod(v, den);
+    if (lt(v, quo(den, INT2FIX(2))))
+        return time_add(tobj, v, -1);
+    else
+        return time_add(tobj, sub(den, v), 1);
+}

 /*
  *  call-seq:
@@ -3918,6 +3976,8 @@ Init_Time(void)
     rb_define_method(rb_cTime, "-", time_minus, 1);

     rb_define_method(rb_cTime, "succ", time_succ, 0);
+    rb_define_method(rb_cTime, "round", time_round, -1);
+
     rb_define_method(rb_cTime, "sec", time_sec, 0);
     rb_define_method(rb_cTime, "min", time_min, 0);
     rb_define_method(rb_cTime, "hour", time_hour, 0);
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 26871)
+++ test/ruby/test_time.rb	(working copy)
@@ -576,4 +576,37 @@ class TestTime < Test::Unit::TestCase
     assert_kind_of(Rational, Time.new(2000,1,1,0,0,Rational(4,3)).to_r)
     assert_kind_of(Rational, Time.utc(1970).to_r)
   end
+
+  def test_round
+    t = Time.utc(1999,12,31, 23,59,59)
+    t2 = (t+0.4).round
+    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
+    assert_equal(0, t2.subsec)
+    t2 = (t+0.49).round
+    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
+    assert_equal(0, t2.subsec)
+    t2 = (t+0.5).round
+    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
+    assert_equal(0, t2.subsec)
+    t2 = (t+1.4).round
+    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
+    assert_equal(0, t2.subsec)
+    t2 = (t+1.49).round
+    assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
+    assert_equal(0, t2.subsec)
+    t2 = (t+1.5).round
+    assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
+    assert_equal(0, t2.subsec)
+
+    t2 = (t+0.123456789).round(4)
+    assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
+    assert_equal(Rational(1235,10000), t2.subsec)
+
+    off = 0.0
+    100.times {|i|
+      t2 = (t+off).round(1)
+      assert_equal(Rational(i % 10, 10), t2.subsec)
+      off += 0.1
+    }
+  end
 end
-- 
[田中 哲][たなか あきら][Tanaka Akira]

In This Thread

Prev Next