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

Conversation

ahacking
Copy link

@ahacking ahacking commented Sep 9, 2014

The following percentage based calculations now work as shown:
Unit('50 %') * Unit('50 %) == Unit('25 %')
Unit('50 m') * Unit('10 %) == Unit('5 m')
Unit('10 %') * Unit('50 m) == Unit('5 m')
Unit('50 m') / Unit('10 %) == Unit('500 m')
Unit('50 %') / Unit('10 m) == Unit('1/20 m')
Unit('50 %') / Unit('50 %) == Unit(1)
Unit('50 %').divmod Unit('50 %) == [1,0]
Unit('15 m').divmod Unit('6 %) == [Unit('250 m'), Unit('0 m')]

A side effect of this enhancement is divmod now also works correctly with the same unit support as regular division:
Unit('15 m').divmod Unit('6') == [Unit('2 m'), Unit('3 m')]
Unit('15 m').divmod Unit('6 cm') == [Unit('2 m/cm'), Unit('3 m/cm')]

 - A side effect of this enhancement is divmod now also works
   correctly with the same unit support as regular division.
@ahacking
Copy link
Author

ahacking commented Jan 6, 2015

ping @olbrich

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?'

@mathieujobin
Copy link

this is pretty cool !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants