omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / temperature-conversion / temperature-conversion.om
| 1 | note Rosetta Code task "Temperature conversion": https://rosettacode.org/wiki/Temperature_conversion |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text tk "K " |
| 6 | text tc "C " |
| 7 | text tf "F " |
| 8 | text tr "R " |
| 9 | text minus "-" |
| 10 | text dot "." |
| 11 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 12 | fn emit a view uchar * to void |
| 13 | o = ulong 0 |
| 14 | while lt o len a bound 4096 |
| 15 | k = write 1 a o sub len a o |
| 16 | if le k 0 |
| 17 | exit 1 |
| 18 | end |
| 19 | o = add o ulong k |
| 20 | end |
| 21 | end |
| 22 | note emitu writes n in decimal; 20 digits hold every ulong |
| 23 | fn emitu n ulong to void |
| 24 | d = new uchar 20 0 |
| 25 | i = ulong 20 |
| 26 | v = n |
| 27 | while true bound 20 |
| 28 | i = sub i 1 |
| 29 | put d i uchar add rem v 10 48 |
| 30 | v = div v 10 |
| 31 | if eq v 0 |
| 32 | break |
| 33 | end |
| 34 | end |
| 35 | emit part d i sub 20 i |
| 36 | end |
| 37 | note emitc2 writes v hundredths (signed) as a decimal with two places |
| 38 | fn emitc2 v long to void |
| 39 | u = ulong 0 |
| 40 | if lt v 0 |
| 41 | emit minus |
| 42 | u = ulong neg v |
| 43 | else |
| 44 | u = ulong v |
| 45 | end |
| 46 | emitu div u 100 |
| 47 | emit dot |
| 48 | f = rem u 100 |
| 49 | if lt f 10 |
| 50 | emitu 0 |
| 51 | end |
| 52 | emitu f |
| 53 | end |
| 54 | note k is kelvin in hundredths, a multiple of 5 so every scale below is exact in hundredths: |
| 55 | note C = K - 273.15, F = K * 9/5 - 459.67, R = K * 9/5 |
| 56 | fn show k long to void |
| 57 | emit tk |
| 58 | emitc2 k |
| 59 | emit nl |
| 60 | emit tc |
| 61 | emitc2 sub k 27315 |
| 62 | emit nl |
| 63 | emit tf |
| 64 | emitc2 sub div mul k 9 5 45967 |
| 65 | emit nl |
| 66 | emit tr |
| 67 | emitc2 div mul k 9 5 |
| 68 | emit nl |
| 69 | emit nl |
| 70 | end |
| 71 | fn main to int |
| 72 | show 2100 |
| 73 | show 0 |
| 74 | show 27315 |
| 75 | show 37315 |
| 76 | show 31015 |
| 77 | ret 0 |
| 78 | end |