Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I define Celsius and Fahrenheit? #13

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

How do I define Celsius and Fahrenheit? #13

kunalsheth opened this issue Nov 24, 2019 · 1 comment

Comments

@kunalsheth
Copy link
Owner

@kunalsheth kunalsheth commented Nov 24, 2019

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
}
@kunalsheth

This comment has been minimized.

Copy link
Owner Author

@kunalsheth kunalsheth commented Nov 24, 2019

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.