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

Make percentage units work like a unitless scalar. #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,16 @@ def *(other)
case other
when Unit
raise ArgumentError, "Cannot multiply by temperatures" if [other, self].any? { |x| x.is_temperature? }
opts = RubyUnits::Unit.eliminate_terms(@scalar*other.scalar, @numerator + other.numerator, @denominator + other.denominator)
opts.merge!(:signature => @signature + other.signature)
return RubyUnits::Unit.new(opts)
if other.numerator == ['<percent>']
return RubyUnits::Unit.new(:scalar => @scalar*other.to_base.scalar, :numerator => @numerator, :denominator => @denominator, :signature => @signature)
elsif @numerator == ['<percent>']
#return other * self
return RubyUnits::Unit.new(:scalar => self.to_base.scalar*other.scalar, :numerator => other.numerator, :denominator => other.denominator, :signature => other.signature)
else
opts = RubyUnits::Unit.eliminate_terms(@scalar*other.scalar, @numerator + other.numerator, @denominator + other.denominator)
opts.merge!(:signature => @signature + other.signature)
return RubyUnits::Unit.new(opts)
end
when Numeric
return RubyUnits::Unit.new(:scalar => @scalar*other, :numerator => @numerator, :denominator => @denominator, :signature => @signature)
else
Expand All @@ -801,9 +808,17 @@ def /(other)
when Unit
raise ZeroDivisionError if other.zero?
raise ArgumentError, "Cannot divide with temperatures" if [other, self].any? { |x| x.is_temperature? }
opts = RubyUnits::Unit.eliminate_terms(@scalar/other.scalar, @numerator + other.denominator, @denominator + other.numerator)
opts.merge!(:signature => @signature - other.signature)
return RubyUnits::Unit.new(opts)
self_pct = @numerator == ['<percent>']
other_pct = other.numerator == ['<percent>']
if other_pct && ! self_pct
return RubyUnits::Unit.new(:scalar => @scalar/other.to_base.scalar, :numerator => @numerator, :denominator => @denominator, :signature => @signature)
elsif self_pct && ! other_pct
return RubyUnits::Unit.new(:scalar => self.to_base.scalar/other.scalar, :numerator => other.numerator, :denominator => other.denominator, :signature => other.signature)
else
opts = RubyUnits::Unit.eliminate_terms(@scalar/other.scalar, @numerator + other.denominator, @denominator + other.numerator)
opts.merge!(:signature => @signature - other.signature)
return RubyUnits::Unit.new(opts)
end
when Numeric
raise ZeroDivisionError if other.zero?
return RubyUnits::Unit.new(:scalar => @scalar/other, :numerator => @numerator, :denominator => @denominator, :signature => @signature)
Expand All @@ -819,11 +834,38 @@ def /(other)
# @param [Object] other
# @return [Array]
def divmod(other)
raise ArgumentError, "Incompatible Units ('#{self}' not compatible with '#{other}')" unless self =~ other
if self.units == other.units
return self.scalar.divmod(other.scalar)
else
return self.to_base.scalar.divmod(other.to_base.scalar)
case other
when Unit
raise ZeroDivisionError if other.zero?
self_pct = @numerator == ['<percent>']
other_pct = other.numerator == ['<percent>']
Copy link
Owner

Choose a reason for hiding this comment

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

I'd prefer to have a helper method called 'percent?'

if other_pct && ! self_pct
return @scalar.divmod(other.to_base.scalar).map {|part|
RubyUnits::Unit.new(:scalar => part, :numerator => @numerator, :denominator => @denominator, :signature => @signature)
}
elsif self_pct && ! other_pct
return self.base.scalar.divmod(other).map {|part|
RubyUnits::Unit.new(:scalar => part, :numerator => other.denominator, :denominator => other.numerator, :signature => other.signature)
}
else
raise ArgumentError, "Incompatible Units ('#{self}' not compatible with '#{other}')" unless self =~ other
if self.units == other.units
return @scalar.divmod(other.scalar)
else
opts = RubyUnits::Unit.eliminate_terms(0, @numerator + other.denominator, @denominator + other.numerator)
opts.merge!(:signature => @signature - other.signature)
return self.to_base.scalar.divmod(other.to_base.scalar).map {|part|
RubyUnits::Unit.new(opts.merge(:scalar => part))
}
end
end
when Numeric
return self.scalar.divmod(other).map {|part|
RubyUnits::Unit.new(:scalar => part, :numerator => @numerator, :denominator => @denominator, :signature => @signature)
}
else
x, y = coerce(other)
return y.divmod x
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/ruby-units/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@

context '#divmod' do
specify { Unit("5 mm").divmod(Unit("2 mm")).should == [2, 1] }
specify { Unit("1 km").divmod(Unit("2 m")).should == [500, 0] }
specify { Unit("1 km").divmod(Unit("2 m")).should == [Unit('500 km/m'), Unit('0 km/m')] }
Copy link
Owner

Choose a reason for hiding this comment

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

This is wrong. This test says that if you divide '1000 m' by '2 m', you should get [500_000, 0] instead of [500, 0].

Note that Unit('500 km/m').to_base #=> 500000

Also note that Unit('50%').to_base #=> 1/2

specify { expect { Unit('1 m').divmod(Unit('2 kg')) }.to raise_error(ArgumentError, "Incompatible Units ('1 m' not compatible with '2 kg')") }
end

Expand Down