-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #615 from sharkdp/fix-unit_of-crashes
Fix unit_of-related crashes
- Loading branch information
Showing
5 changed files
with
25 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
# unit_of | ||
|
||
assert_eq(unit_of(0), 1) | ||
|
||
assert_eq(unit_of(1), 1) | ||
assert_eq(unit_of(1.2345), 1) | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
use core::scalar | ||
|
||
@description("Extract the unit of a quantity (the `km/h` in `20 km/h`). This can be useful in generic code, but should generally be avoided otherwise.") | ||
@example("unit_of(20 km/h)") | ||
fn unit_of<T: Dim>(x: T) -> T | ||
use core::error | ||
|
||
@description("Extract the plain value of a quantity (the `20` in `20 km/h`). This can be useful in generic code, but should generally be avoided otherwise.") | ||
@example("value_of(20 km/h)") | ||
fn value_of<T: Dim>(x: T) -> Scalar = x / unit_of(x) | ||
fn value_of<T: Dim>(x: T) -> Scalar | ||
|
||
@description("Extract the unit of a quantity (the `km/h` in `20 km/h`). This can be useful in generic code, but should generally be avoided otherwise. Returns an error if the quantity is zero.") | ||
@example("unit_of(20 km/h)") | ||
fn unit_of<T: Dim>(x: T) -> T = if x_value == 0 then error("…") else x / value_of(x) | ||
where x_value = value_of(x) |
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