From 4dd3c9650f8fd47a709204a52a32ddbf9ae1221c Mon Sep 17 00:00:00 2001 From: Shane Caldwell Date: Sun, 21 Mar 2021 18:19:47 -0700 Subject: [PATCH] Add Kelvin temperature type and converters --- ch2/tempconv/conv.go | 12 ++++++++++++ ch2/tempconv/tempconv.go | 2 ++ 2 files changed, 14 insertions(+) diff --git a/ch2/tempconv/conv.go b/ch2/tempconv/conv.go index 1ef1f9187..06991007e 100644 --- a/ch2/tempconv/conv.go +++ b/ch2/tempconv/conv.go @@ -10,7 +10,19 @@ package tempconv // CToF converts a Celsius temperature to Fahrenheit. func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32) } +// CToK converts a Celsius temperature to Kelvin. +func CToK(c Celsius) Kelvin { return Kelvin(c - Celsius(AbsoluteZeroC)) } + // FToC converts a Fahrenheit temperature to Celsius. func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9) } +// FToK converts a Fahrenheit temperature to Kelvin. +func FToK(f Fahrenheit) Kelvin { return Kelvin(CToF(FToC(f))) } + +// KToC converts a Kelvin temperature to Celsius. +func KToC(k Kelvin) Celsius { return Celsius(k + Kelvin(AbsoluteZeroC)) } + +// KToF converts a Kelvin temperature to Fahrenheit. +func KToF(k Kelvin) Fahrenheit { return Fahrenheit(CToF(KToC(k))) } + //!- diff --git a/ch2/tempconv/tempconv.go b/ch2/tempconv/tempconv.go index 5af1b6dee..1fed9ba07 100644 --- a/ch2/tempconv/tempconv.go +++ b/ch2/tempconv/tempconv.go @@ -10,6 +10,7 @@ import "fmt" type Celsius float64 type Fahrenheit float64 +type Kelvin float64 const ( AbsoluteZeroC Celsius = -273.15 @@ -19,5 +20,6 @@ const ( func (c Celsius) String() string { return fmt.Sprintf("%g°C", c) } func (f Fahrenheit) String() string { return fmt.Sprintf("%g°F", f) } +func (k Kelvin) String() string { return fmt.Sprintf("%g K", k) } //!-