Skip to content

Commit cc53124

Browse files
committed
fix: resolve multiple_of validation bug for negative floats using round-based approach
1 parent 7cface5 commit cc53124

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

src/validators/float.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,10 @@ impl Validator for ConstrainedFloatValidator {
109109
return Err(ValError::new(ErrorTypeDefaults::FiniteNumber, input));
110110
}
111111
if let Some(multiple_of) = self.multiple_of {
112-
const EPSILON_FACTOR: f64 = 100.0;
113-
let epsilon_threshold = f64::EPSILON * EPSILON_FACTOR;
114-
115-
// Round the result of dividing the input value by the multiple
116-
let rounded = (float / multiple_of).round();
117-
118-
// Calculate the difference between the rounded value and the original value
119-
let diff = (float - rounded * multiple_of).abs();
120-
121-
// Calculate the relative error (avoid division by zero)
122-
let relative_error = if float != 0.0 { diff / float.abs() } else { 0.0 };
123-
124-
// Threshold (considering both relative and absolute error)
125-
let threshold = epsilon_threshold.max(multiple_of * epsilon_threshold);
126-
127-
// Check if the difference exceeds the threshold and the relative error is significant
128-
if diff > threshold && relative_error > epsilon_threshold {
112+
let tolerance = 1e-9;
113+
let rounded_div = (float / multiple_of).round();
114+
let diff = (float - (rounded_div * multiple_of)).abs();
115+
if diff > tolerance {
129116
return Err(ValError::new(
130117
ErrorType::MultipleOf {
131118
multiple_of: multiple_of.into(),

0 commit comments

Comments
 (0)