Skip to content

Commit 1f44b20

Browse files
authored
Fix unitless addition and subtraction (#175)
When adding or subtracting two unitless numbers, the result used to be converted to the same units as the receiver. This didn't work if the receiver was unitless. The fix changes the way we do the conversion in a way that is not subject to this problem.
1 parent 8c3b1fb commit 1f44b20

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/ruby_units/unit.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ def +(other)
847847
RubyUnits::Unit.new(scalar: (other.scalar + convert_to(other.temperature_scale).scalar), numerator: other.numerator, denominator: other.denominator, signature: other.signature)
848848
end
849849
else
850-
RubyUnits::Unit.new(scalar: (base_scalar + other.base_scalar), numerator: base.numerator, denominator: base.denominator, signature: @signature).to(units)
850+
RubyUnits::Unit.new(scalar: (base_scalar + other.base_scalar), numerator: base.numerator, denominator: base.denominator, signature: @signature).convert_to(self)
851851
end
852852
else
853853
raise ArgumentError, "Incompatible Units ('#{self}' not compatible with '#{other}')"
@@ -883,7 +883,7 @@ def -(other)
883883
elsif other.temperature?
884884
raise ArgumentError, 'Cannot subtract a temperature from a differential degree unit'
885885
else
886-
RubyUnits::Unit.new(scalar: (base_scalar - other.base_scalar), numerator: base.numerator, denominator: base.denominator, signature: @signature).to(units)
886+
RubyUnits::Unit.new(scalar: (base_scalar - other.base_scalar), numerator: base.numerator, denominator: base.denominator, signature: @signature).convert_to(self)
887887
end
888888
else
889889
raise ArgumentError, "Incompatible Units ('#{self}' not compatible with '#{other}')"

spec/ruby_units/unit_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,10 @@
17441744
context 'between a degree and a temperature' do
17451745
specify { expect(RubyUnits::Unit.new('100 degK') + RubyUnits::Unit.new('100 tempK')).to eq(RubyUnits::Unit.new('200 tempK')) }
17461746
end
1747+
1748+
context 'between two unitless units' do
1749+
specify { expect(RubyUnits::Unit.new('1') + RubyUnits::Unit.new('2')).to eq 3 }
1750+
end
17471751
end
17481752

17491753
context 'subtracting (-)' do
@@ -1784,6 +1788,10 @@
17841788
context 'between a degree and a temperature' do
17851789
specify { expect { (RubyUnits::Unit.new('100 degK') - RubyUnits::Unit.new('100 tempK')) }.to raise_error(ArgumentError, 'Cannot subtract a temperature from a differential degree unit') }
17861790
end
1791+
1792+
context 'a unitless unit from another unitless unit' do
1793+
specify { expect(RubyUnits::Unit.new('1') - RubyUnits::Unit.new('2')).to eq -1 }
1794+
end
17871795
end
17881796

17891797
context 'multiplying (*)' do

0 commit comments

Comments
 (0)