Skip to content

High precision BigDecimal to Rational conversion wildly inaccurate #4711

@joshwlewis

Description

@joshwlewis

Environment

  • Darwin 16.6.0 (MacOS Sierra 10.12.5)
  • jruby 9.1.8.0 (2.3.1) 2017-03-06 90fc7ab Java HotSpot(TM) 64-Bit Server VM 25.131-b11 on 1.8.0_131-b11 +jit [darwin-x86_64]

Expected Behavior

In each MRI version I've tried, I am able to convert a high-precision BigDecimal to a reasonably similar Rational value. For instance, in MRI 2.4.1:

require 'bigdecimal'
# => true

BigDecimal.new("3.141592653589793238462643383279502884197169").to_r
# => (3141592653589793238462643383279502884197169/1000000000000000000000000000000000000000000)

_.to_f
# => 3.141592653589793

Actual Behavior

require 'bigdecimal'
# => true

BigDecimal.new("3.141592653589793238462643383279502884197169").to_r
# => (448798950512827605494663340468500412028167/1317624576693539401)

_.to_f
# => 3.4061215800865547e+23

I'd expect something in the region of 3.14 here, but instead, the result is approximately 340 sextillion.

Other info

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions