diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2021-10-26 22:44:17 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2021-10-26 23:12:17 -0700 |
commit | 5713eb7302a11af489faeb3172cc376398db57e0 (patch) | |
tree | aa37dd2c8d926f5c932bb0b9619d12ec105823a4 /tutorial/task9-hint2.mu | |
parent | e419abe61579ecebb6055a19cec1e35e0538df9a (diff) | |
download | mu-5713eb7302a11af489faeb3172cc376398db57e0.tar.gz |
make room for a task before fractional numbers
Diffstat (limited to 'tutorial/task9-hint2.mu')
-rw-r--r-- | tutorial/task9-hint2.mu | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tutorial/task9-hint2.mu b/tutorial/task9-hint2.mu new file mode 100644 index 00000000..97ccfc51 --- /dev/null +++ b/tutorial/task9-hint2.mu @@ -0,0 +1,60 @@ +fn to-km miles: float -> _/xmm1: float { + var result/xmm1: float <- copy miles + # fill in the blanks to compute miles * 1.609 + # 1.609 = 1609 / 1000 (use function `rational`) + # 1609 = 0x649 in hex + # 1000 = 0x3e8 in hex + + return result +} + +fn test-to-km { + # 0 miles = 0 km + var zero: float # Mu implicitly initializes variables in memory to 0 + var result/xmm1: float <- to-km zero + compare result, zero + { + break-if-= + draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "F - 0 miles = 0 km\n", 3/fg 0/bg + count-test-failure + } + # 1 mile = 1.609 km approximately + var one/eax: int <- copy 1 + var one-float/xmm0: float <- convert one + result <- to-km one-float + var lower-bound/xmm0: float <- rational 0x649, 0x3e8 # 1609/1000 in hex + { + compare result, lower-bound + break-if-float>= + draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "F - 1 mile > 1.609 km\n", 3/fg 0/bg + count-test-failure + } + var upper-bound/xmm0: float <- rational 0x64a, 0x3e8 # 1610/1000 in hex + { + compare result, upper-bound + break-if-float<= + draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "F - 1 mile < 1.610 km\n", 3/fg 0/bg + count-test-failure + } + # 2 miles = 3.218 km approximately + var two/eax: int <- copy 2 + var two-float/xmm0: float <- convert two + result <- to-km two-float + var lower-bound/xmm0: float <- rational 0xc92, 0x3e8 # 3218/1000 in hex + { + compare result, lower-bound + break-if-float>= + draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "F - 2 miles > 3.218 km\n", 3/fg 0/bg + count-test-failure + } + var upper-bound/xmm0: float <- rational 0xc93, 0x3e8 # 3219/1000 in hex + { + compare result, upper-bound + break-if-float<= + draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "F - 2 miles < 3.219 km\n", 3/fg 0/bg + count-test-failure + } +} + +fn main { +} |