Skip to content

Commit 7cfe4d0

Browse files
committed
fix: handle RangeError in unit conversion and ensure proper normalization
1 parent 11bff0c commit 7cfe4d0

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

lib/ruby_units/unit.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ def self.normalize_to_i(value)
425425
else
426426
value
427427
end
428+
rescue RangeError
429+
# This can happen when a Complex number with a non-zero imaginary part is provided, or when value is Float::NAN or
430+
# Float::INFINITY
431+
value
428432
end
429433

430434
# Parse a string consisting of a number and a unit string
@@ -1293,7 +1297,7 @@ def convert_to(other)
12931297
converted_value = conversion_scalar * (source_numerator_values + target_denominator_values).reduce(1, :*) / (target_numerator_values + source_denominator_values).reduce(1, :*)
12941298
# Convert the scalar to an Integer if the result is equivalent to an
12951299
# integer
1296-
converted_value = unit_class.normalize_to_i(converted_value) if scalar_is_integer
1300+
converted_value = unit_class.normalize_to_i(converted_value)
12971301
unit_class.new(scalar: converted_value, numerator: target_num, denominator: target_den, signature: target.signature)
12981302
end
12991303
end

0 commit comments

Comments
 (0)