Skip to content

Instantly share code, notes, and snippets.

@casperisfine
Created July 27, 2021 21:55
Show Gist options
  • Save casperisfine/f3f7b30d0ce4b4f41bc19eae097143b1 to your computer and use it in GitHub Desktop.
Save casperisfine/f3f7b30d0ce4b4f41bc19eae097143b1 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bigdecimal/util"
# Ref: https://github.com/ruby/ruby/commit/e1424c35012ccf1f3822c9046081b225aef4142e
# Ref: https://github.com/ruby/bigdecimal/issues/70
# Ruby 3.0 changed the default precision used when calling `Float#to_d` from 15 to 16.
# It causes a whole lot of issues. I'm trying to figure out what can be done about it
# but in the meantime the only solution I see is to revert it back to 2.x behavior.
if RUBY_VERSION >= "3.0"
class Float
def to_d(precision = Float::DIG)
BigDecimal(self, precision)
end
end
end
@andxyz
Copy link

andxyz commented Nov 2, 2022

Haha, I just had to write this myself, thank you for posting it. I work at a rails company now moving to ruby3.

I ended up stumbling here from rails/rails#42098 (comment)

I believe the issue still exists in ruby v3.1.2

thanks again @casperisfine

@casperisfine
Copy link
Author

I believe the issue still exists in ruby v3.1.2

I don't think so, we were able to remove the patch when we upgraded to 3.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment