Skip to content

Difference in Float() hex parsing between JRuby and CRuby #9044

Description

@kaisecheng

Environment Information

jruby 9.4.14.0 (3.1.7) 2025-08-28 ddda6d5 OpenJDK 64-Bit Server VM 21.0.4+7-LTS on 21.0.4+7-LTS +indy +jit [arm64-darwin]

Description

In CRuby, Float() correctly parses signed (e.g. "-0x7b") and uppercase (e.g. "0X1A") hexadecimal strings, but JRuby raises ArgumentError

Expected Behavior

In CRuby, ruby 3.3.1 (2024-04-23 revision c56cd86388) [arm64-darwin24]

irb(main):001> Float("0x7b")
=> 123.0
irb(main):002> Float("-0x7b")
=> -123.0
irb(main):003> Float("0x1A")
=> 26.0
irb(main):004> Float("0X1A")
=> 26.0

Actual Behavior

In Jruby,

irb(main):001:0> Float("0x7b")
=> 123.0
irb(main):002:0> Float("-0x7b")
org/jruby/RubyKernel.java:551:in `Float': invalid value for Float(): "-0x7b" (ArgumentError)
	from org/jruby/RubyKernel.java:392:in `Float'
	from (irb):2:in `evaluate'
	from org/jruby/RubyKernel.java:1277:in `eval'
	from org/jruby/RubyKernel.java:1725:in `loop'
	from org/jruby/RubyKernel.java:1426:in `catch'
	from org/jruby/RubyKernel.java:1426:in `catch'
	from /Users/kcheng/.rbenv/versions/jruby-9.4.14.0/lib/ruby/gems/shared/gems/irb-1.4.2/exe/irb:11:in `<main>'
	from org/jruby/RubyKernel.java:1223:in `load'
	from /Users/kcheng/.rbenv/versions/jruby-9.4.14.0/bin/irb:25:in `<main>'
irb(main):003:0> Float("0x1A")
=> 26.0
irb(main):004:0> Float("0X1A")
org/jruby/RubyKernel.java:551:in `Float': invalid value for Float(): "0X1A" (ArgumentError)
	from org/jruby/RubyKernel.java:392:in `Float'
	from (irb):4:in `evaluate'
	from org/jruby/RubyKernel.java:1277:in `eval'
	from org/jruby/RubyKernel.java:1725:in `loop'
	from org/jruby/RubyKernel.java:1426:in `catch'
	from org/jruby/RubyKernel.java:1426:in `catch'
	from /Users/kcheng/.rbenv/versions/jruby-9.4.14.0/lib/ruby/gems/shared/gems/irb-1.4.2/exe/irb:11:in `<main>'
	from org/jruby/RubyKernel.java:1223:in `load'
	from /Users/kcheng/.rbenv/versions/jruby-9.4.14.0/bin/irb:25:in `<main>'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions