# How do I define Celsius and Fahrenheit? #13

Open
opened this issue Nov 24, 2019 · 1 comment
Open

# How do I define Celsius and Fahrenheit?#13

opened this issue Nov 24, 2019 · 1 comment

### kunalsheth commented Nov 24, 2019 • edited

 UPDATE: DO NOT DO THIS. units-of-measure is incompatible with Celsius and Fahrenheit. Attempting to define Celsius and Fahrenheit will cause weird and incorrect behavior. Celsius and Fahrenheit would have to be manually programmed in on the user's end. Celsius and Fahrenheit aren't "units" in the theoretical sense because they have different zeros. To add Celsius and Fahrenheit, your code would look like the following: ```inline val Number.Celsius get() = `Θ`(toDouble() + 273.15) inline val `Θ`.Celsius get() = siValue - 273.15 object `Celsius` : UomConverter<`Θ`>, Quan<`Θ`> by box(1.Celsius) { override val unitName = "Celsius" override fun invoke(x: Double) = x.Celsius override fun invoke(x: `Θ`) = x.Celsius } inline val Number.Fahrenheit get() = `Θ`((toDouble() - 32) * 5 / 9 + 273.15) inline val `Θ`.Fahrenheit get() = (siValue - 273.15) * 9 / 5 + 32 object Fahrenheit : UomConverter<`Θ`>, Quan<`Θ`> by box(1.Fahrenheit) { override val unitName = "Fahrenheit" override fun invoke(x: Double) = x.Fahrenheit override fun invoke(x: `Θ`) = x.Fahrenheit }```

### Celsius and Fahrenheit are EXTREMELY BROKEN!

Due to operator precedence:

```0.Celsius == 273.15.Kelvin // correct
-0.Celsius == -273.15.Kelvin // should be false!```

units-of-measure does not know how to treat units with shifted zeros:

```10.Fahrenheit - 3.Fahrenheit == -452.67.Fahrenheit // should be false!

10.Fahrenheit == 260.928.Kelvin // correct
3.Fahrenheit == 257.039.Kelvin // correct
260.928.Kelvin - 257.039.Kelvin == -452.67.Fahrenheit // correct```