Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bug#13833 String#scanf("%a") incorrectly requires a sign on the (binary) exponent #1689

Closed
wants to merge 2 commits into from

Conversation

tarotaro0
Copy link

Issue

Implementation

  • When we use String#scanf("%a"), it doesn't require a sign on the (binary) exponent similar to Float class.

Before :

>> "0x1p+9".scanf("%a")
=> [512.0]
>> "0x1p9".scanf("%a")
=> [0.0]  

After :

>> "0x1p+9".scanf("%a")
=> [512.0]
>> "0x1p9".scanf("%a")
=> [512.0]  

@rhenium
Copy link
Member

rhenium commented Aug 31, 2017

It looks like (decimal-)exponent-part shares the same bug. Would you mind fixing this as well?

$ ruby -rscanf -e'p "1.e+10".scanf("%a")'
[10000000000.0]
$ ruby -rscanf -e'p "1.e10".scanf("%a")'
[1.0]

@tarotaro0
Copy link
Author

tarotaro0 commented Aug 31, 2017

I would fix (decimal-) exponent-part.

$ ruby -rscanf -e'p "1.0e+10".scanf("%a")'
[10000000000.0]
$ ruby -rscanf -e'p "1.0e10".scanf("%a")'
[10000000000.0]

but it failed this test :

TestIOScanf#test_"12.e23 45 string"("%f %*d %s") [/ruby/test/scanf/test_scanf.rb:303]:
<[12.0, "string"]> expected but was
<[1.2e+24, "string"]>.

This failure happens in /test/scanf/test_scanf.rb:225 :

[ "%f %*d %s", "12.e23 45 string", ["12.e23".to_f, "string"] ],

I think this cause is String#to_f :

irb(main):001:0> "12.e23".to_f
=> 12.0
irb(main):002:0> "12.0e23".to_f
=> 1.2e+24

I think this behavior is not expected, so I fixed test_scanf.rb as below :

Before :

[ "%f %*d %s", "12.e23 45 string", ["12.e23".to_f, "string"] ]

After :

[ "%f %*d %s", "12.0e23 45 string", ["12.0e23".to_f, "string"] ]

@tarotaro0
Copy link
Author

I discussed with @matz and ruby committers directly about this problem.

This includes two problems.

  1. String#scanf("%a") (decimal-) exponent-part incorrectly requires a sign on the (binary) exponent.

like below :

$ ruby -rscanf -e'p "1.e+10".scanf("%a")'
[10000000000.0]
$ ruby -rscanf -e'p "1.e10".scanf("%a")'
[1.0]
  1. String#to_f parses .e~~ as a method.

like below :

irb(main):001:0> "12.e23".to_f
=> 12.0
irb(main):002:0> "12.0e23".to_f
=> 1.2e+24

I think this problem should be created as new issues.

After the second problem is fixed, then the first problem should be fixed I think.

So I revert the change about only (decimal-) exponent-part, and I'll create new issues after merge this PR.

@tarotaro0 tarotaro0 changed the title Fixed Bug#13833 Fix Bug#13833 Sep 1, 2017
@sonots sonots changed the title Fix Bug#13833 Fix Bug#13833 String#scanf("%a") incorrectly requires a sign on the (binary) exponent Sep 29, 2017
@matzbot matzbot closed this in 4bf6a04 Oct 21, 2017
mrkn pushed a commit to mrkn/ruby that referenced this pull request Dec 1, 2017
* lib/scanf.rb (extract_float, initialize): allow to omit a sign
  on the binary exponent.  [ruby-core:82435] [Bug #13833]
  [Fix rubyGH-1689]

From: tarotaro0 <tarousann11922960@yahoo.co.jp>

git-svn-id: svn+ssh://svn.ruby-lang.org/ruby/trunk@60261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants