Fix numericality equality validation on floats#32852
Fix numericality equality validation on floats#32852rafaelfranca merged 1 commit intorails:masterfrom
Conversation
|
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @schneems (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
ec8b4ef to
797dad9
Compare
There was a problem hiding this comment.
Add .freeze at the end of constants.
There was a problem hiding this comment.
Regexes in ruby are immutable so I actually don't need to do that. TIL
There was a problem hiding this comment.
And no, we don't freeze constants just because they are constants. This is defensive programming and have no benefits at all in terms of runtime. If people wants to mutate a constant it is not a freeze that will stop them.
There was a problem hiding this comment.
I know it's what was here before, but I'm a bit suspicious of the performance of this vs using something like =~ or match. Would be worth a benchmark to test IMO.
There was a problem hiding this comment.
Warming up --------------------------------------
=== 142.302k i/100ms
=~ 139.718k i/100ms
#match 139.561k i/100ms
Calculating -------------------------------------
=== 2.230M (± 2.3%) i/s - 11.242M in 5.044579s
=~ 2.238M (± 2.5%) i/s - 11.317M in 5.060422s
#match 2.167M (± 3.0%) i/s - 10.886M in 5.027772s
I think they're all relatively similar. Let's keep using ===.
There was a problem hiding this comment.
TIL. Thanks for the benchmark.
There was a problem hiding this comment.
I prefer the readability of positives parse_as_number(raw_value).present? vs what's here before, but with a quick benchmark to make sure it doesn't impact performance.
There was a problem hiding this comment.
That does read better, but I think because the raw_value can be an anything (including another ActiveRecord object by mistake), its best not to use #present? for performance reasons.
There was a problem hiding this comment.
Might be worth separating this out into two or more tests. Feels kinda smushed as-is right now.
c66d14a to
710066b
Compare
81349ed to
e54df31
Compare
|
@gmcgibbon please rebase this!!! |
e54df31 to
ea07a65
Compare
|
Done! |
There was a problem hiding this comment.
Too many returns here. I'd go with a if/else so it is explicit all the branches.
There was a problem hiding this comment.
There is an extra space after the !
ea07a65 to
d126c0d
Compare
Fix numericality equality validation on floats
Test case which was added at rails#32852 passes in Active Model but doesn't pass in Active Record since rails#38210 due to `Float::DIG` has changed to `BigDecimal.double_fig`. ```ruby irb(main):001:0> require 'bigdecimal/util' => true irb(main):002:0> 65.6.to_d => 0.656e2 irb(main):003:0> 65.6.to_d(Float::DIG) => 0.656e2 irb(main):004:0> 65.6.to_d(BigDecimal.double_fig) => 0.6559999999999999e2 ```
Summary
Currently, the numericality validator of active model has trouble with equality of decimal values. Notably, some single decimal floats (eg.
65.6). This PR fixes #26085 by castingFloats toBigDecimals on both ends of the validation.If there's interest in merging this, please let me know how I can improve the decimal regex.