Bug #3978
closedString#scanf (lib/scanf.rb) で '0x2.p+2' に '0x2.0p+2' と同様の解釈が与えらえる
Description
=begin
String#scanf (lib/scanf.rb) で '0x2.p+2' に '0x2.0p+2' と同様の解釈が与えらえる。
'0x2.p+2'.scanf('%e') #=> [8.0]
'0x2.0p+2'.scanf('%e') #=> [8.0]
指数表現ではそのような事はありません。
'2.e+2'.scanf('%e') #=> [2.0]
'2.0e+2'.scanf('%e') #=> [200.0]
=end
Updated by naruse (Yui NARUSE) over 13 years ago
=begin
Scanf#scanf の説明を見ると、
scanf for Ruby is an implementation of the C function scanf(3),$
modified as necessary for Ruby compatibility.
とあります。
ここで、C99 を見ると以下の通りであり、2.e+2 は 200.0 と解釈されるように見えます。
これを 2.0 と解釈するのは「modified as necessary for Ruby compatibility」の範囲外のように思えます。
つまり、わたしは 指数表現の方を直そうと思っているのですが、どう思いますか。
6.4.4.2 Floating constants
Syntax
floating-constant:
decimal-floating-constant
hexadecimal-floating-constant
decimal-floating-constant:
fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt
hexadecimal-floating-constant:
hexadecimal-prefix hexadecimal-fractional-constant
binary-exponent-part floating-suffixopt
hexadecimal-prefix hexadecimal-digit-sequence
binary-exponent-part floating-suffixopt
fractional-constant:
digit-sequenceopt . digit-sequence
digit-sequence .
exponent-part:
e signopt digit-sequence
E signopt digit-sequence
=end
Updated by tadf (tadayoshi funaba) over 13 years ago
=begin
ここで、C99 を見ると以下の通りであり、2.e+2 は 200.0 と解釈されるように見えます。
これを 2.0 と解釈するのは「modified as necessary for Ruby compatibility」の範囲外のように思えます。
つまり、わたしは 指数表現の方を直そうと思っているのですが、どう思いますか。
sprintf との関係からすれば、そのほうが自然かもしれません。%e のほうが間
違いだった、という意見に異論がなければそれでいいと思います。
=end
Updated by naruse (Yui NARUSE) over 13 years ago
- Status changed from Open to Closed
- % Done changed from 0 to 100
=begin
This issue was solved with changeset r29599.
tadayoshi, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
=end