From: "naruse (Yui NARUSE) via ruby-core" Date: 2026-11-12T07:38:18+00:00 Subject: [ruby-core:127433] [Ruby Feature#21308] Replacing the Float#to_s (dtoa.c) implementation with a modern algorithm Issue #21308 has been updated by naruse (Yui NARUSE). akr (Akira Tanaka) wrote in #note-8: > I'm afraid the new implementation's behavior is different from Ruby's. > As far as I remember, Ruby's implementation produces different strings for different floats as follows. > > ``` > % ruby -e '1000.times { n = rand; p n.to_s != n.next_float.to_s }'|uniq > true > ``` > > I'm not sure the new implementation satisfies this property. I add some references about this concern. It is discussed in https://bugs.ruby-lang.org/issues/2152. After some discussion, the consensus is, as far as I understand, "shortest roundtrippable string". The current implementation is introduced at https://github.com/ruby/ruby/commit/07b314f07b0fa45838474b4f8c7ef32c746c6536 It uses mode: 0 of dtoa see also https://netlib.org/fp/dtoa.c > mode: > 0 ==> shortest string that yields d when read in > and rounded to nearest. ---------------------------------------- Feature #21308: Replacing the Float#to_s (dtoa.c) implementation with a modern algorithm https://bugs.ruby-lang.org/issues/21308#change-119879 * Author: watson1978 (Shizuo Fujita) * Status: Open ---------------------------------------- This is a feature request to replace Ruby's `Float#to_s` implementation with a modern high-performance float-to-string conversion algorithm such as Grisu. Currently, `Float#to_s` in Ruby uses the BSD-derived implementation in [missing/dtoa.c](https://github.com/ruby/ruby/blob/master/missing/dtoa.c). Meanwhile, `json` gem has adopted a Grisu-based implementation in [ruby/json#768](https://github.com/ruby/json/pull/768), it has been improve the performance of float-to-string conversion dramatically. It appears to have brought about a 10 times improvement in performance. However, the improvement is limited to the `json` gem. If we can use the same implementation in `Float#to_s`, it will introduce the improvement in all float-to-string conversion cases. (In additional, it would be better if an API could be added that could be called directly from the C extension library, it will reduce the overhead of the call.) -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/