-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix QuantityType arithmetic of mixed units (#147)
This PR addressed the following problems: 1. Operates under the rhs unit ``` a = QuantityType.new("20 °C") b = QuantityType.new("9 °F") logger.warn a + b # => 45 °F - this should be 25 °C logger.warn b + a # => 25 °C - this should be 45 °F ``` self gets converted to other's unit + other converted to self.unit then added to self. This gave the wrong result in the wrong unit. The lhs just needs to be converted to the unit defined by the block, and rhs needs to be converted using `to_unit_relative` to the unit defined by the block. 2. Inside a unit block ``` a = QuantityType.new("20 °C") b = QuantityType.new("9 °F") unit("°C") do logger.warn a + b # => 7.2222 °C => This should be 25 °C logger.warn b + a # => 7.2222 °C => correct. 9 °F + 20 °C -> -12.78 °C + 20 °C logger.warn 2 + b # -> - 20.999 °F => Should be 2°C + 5°C = 7 °C logger.warn 2 - b # => 24.999 °F => Should be 2 - 5 = -3 °C end ``` 3. Multiplications inside a unit block didn't take up the block's unit ``` kw = 5 | "kW" unit("W") do logger.warn (kw * 2).format("%.0f %unit%") # => 10 kW => Should turn into 10000 W logger.warn (2 * kw).format("%.0f %unit%") # => 10 kW => Should turn into 10000 W end ``` 4. +/- against non-quantity type is still allowed. It takes up the other operand's unit. This should raise an exception instead. ``` kw = 5 | "kW" logger.warn kw + 5 # => 10kW logger.warn 5 + kw # => java.lang.NullPointerException: Cannot read field "quantity" because "state" is null ``` 5. Fix failing specs due to core changes in openhab/openhab-core#3792 --------- Signed-off-by: Jimmy Tanagra <[email protected]>
- Loading branch information
Showing
3 changed files
with
130 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters