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

Enable conversion of Float's to BigNum's #52

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kyku
Copy link

@kyku kyku commented Jun 1, 2021

When converting from a Float, the following error is given:

Exception: can't omit precision for a Float.

This PR fixes that.

@codecov
Copy link

codecov bot commented Jun 1, 2021

Codecov Report

Merging #52 (a40f4aa) into master (7c07f11) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #52   +/-   ##
=======================================
  Coverage   99.09%   99.09%           
=======================================
  Files           6        6           
  Lines         331      333    +2     
=======================================
+ Hits          328      330    +2     
  Misses          3        3           
Impacted Files Coverage Δ
lib/sorbet-coerce/converter.rb 97.14% <100.00%> (ø)
spec/coerce_spec.rb 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7c07f11...a40f4aa. Read the comment docs.

@@ -131,7 +131,7 @@ def _convert_simple(value, type, raise_coercion_error)
elsif value.is_a?(type)
return value
elsif type == BigDecimal
return BigDecimal(value)
return BigDecimal(value, 0)
Copy link
Contributor

@diego-silva diego-silva Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not fully sure this is what we would want 🤔

irb(main):001:0> BigDecimal(123.321, 0).to_digits
=> "123.320999999999997953636921010911464691"

maybe we are better off calling to_s on the value?

Suggested change
return BigDecimal(value, 0)
return BigDecimal(value.to_s)
irb(main):002:0> BigDecimal(123.321.to_s).to_digits
=> "123.321"

However, we probably still need to check what to do with nils or things that don't respond to #to_s 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to value.to_s

we probably still need to check what to do with nils or things that don't respond to #to_s

That's already taken care of on L125.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need to be part of this PR, but we should also catch argument errors like

ArgumentError (invalid value for BigDecimal(): "a")

in this method, so the error can be properly re-raised and being treated properly by the raise_coercion_error flag.

Copy link
Contributor

@donaldong donaldong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kyku! 🙏

@@ -131,7 +131,7 @@ def _convert_simple(value, type, raise_coercion_error)
elsif value.is_a?(type)
return value
elsif type == BigDecimal
return BigDecimal(value)
return BigDecimal(value, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to value.to_s

we probably still need to check what to do with nils or things that don't respond to #to_s

That's already taken care of on L125.

@hlascelles
Copy link
Contributor

This would be useful to us as well. @kyku can you make the to_s change (we agree!), and we can get this in? Thanks!

@hlascelles
Copy link
Contributor

Hi @mattxwang, can this be folded in please?

@mattxwang
Copy link
Contributor

Hi @hlascelles, I unfortunately don't work at CZI anymore and don't have write access to this repository. I am not sure who still does, but you could always fork main with this merged in & use it as your gem source until it is!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants