# Temperature Conversion

## Approximation Problem

Everytime I travel to the US, one thing that troubles me a little is having to convert temperature from the Celsius scale to the Fahrenheit scale and vice versa. The exact conversion formulas are: \begin{align*} f & = \frac{9f}{5} + 32, \\ c & = \frac{5(f - 32)}{9} \end{align*} where \( f \) represents the temperature value in Fahrenheit and \( c \) represents the temperature value in Celsius.

While the formulas above are accurate, they are not very convenient to mentally figure what I need to set a room thermostat in Fahrenheit scale to if I want to keep the room at, say, 25 °C. For this particular case, I have memorized that 25 °C is 77 °F. This combined with the fact that every 5 °C interval corresponds to an interval of 9 °F, it is easy to mentally compute that 20 °C is 68 °F or 22.5 °C is 72.5 °F. It would still be nice to find an easy way to mentally convert any arbitrary temperature in one scale to the other scale.

In my last trip to the US, I decided to devise a few approximation methods to convert temperature from the Fahrenheit scale to the Celsius scale and vice versa. I arrived at two methods: one to convert temperature value in Fahrenheit to Celsius and another to convert from Fahrenheit to Celsius. Both these methods are based on the exact conversion formulas but they sacrifice accuracy a little bit in favour of simplifying the computations, so that they can be performed mentally.

## Approximation Methods

This section presents the approximation methods that I have arrived at. They can be performed mentally. To keep the methods convenient enough to perform mentally, we work with integers only. We always start with an integer value in Celsius or Fahrenheit. The result of conversion is also an integer. If a fraction arises in an intermediate step, we discard the fractional part. For example, if a step requires us to calculate one-tenth of a number, say, 25, we consider the result to be 2. Similarly, if a step requires us to halve the number 25, we consider the result to be 12. This is also known as floor division or integer division.

To go from °C to °F, here is my quick three-step approximation method:

- Subtract one-tenth of the value in Celsius from itself.
- Double the previous result.
- Add 31 to the previous result.

The approximation error due to this method does not exceed 1 °F in magnitude. In terms of Celsius, the approximation error does not exceed 0.56 °C. I believe this is pretty good if we are talking about setting the thermostat temperature.

To go from °F to °C, we roughly inverse the above steps:

- Subtract 31 from the value in Fahrenheit.
- Halve the result.
- Add one-tenth of the previous result to itself.

In fact, for integer temperature values between 32 °F (0 °C) and 86 °F (30 °C), the approximation error due to this method does not exceed 1.12 °C. Further, for integer temperature values between -148 °F (-100 °C) and 212 °F (100 °C), the approximation error does not exceed 1.89 °C.

## Examples

Let us do a few examples to see how well the three-step methods above work. Let us say, we want to convert 82 °C to °F.

- Subtract one-tenth of 82 from itself, i.e., subtract 8 from 82. We get 74.
- Double 74. We get 148.
- Add 31 to 148. We get 179.

The exact value for 83 °C is 179.6 °F. The approximation method has underestimated the actual temperature in Fahrenheit by only 0.6 °F.

Let us now try to convert 179 °F to °C.

- Subtract 31 from 179. We get 148.
- Halve 148. We get 74.
- Add one-tenth of 74 to itself, i.e., add 7 to 74. We get 81.

The exact value for 179 °F is 81.7 °C, so this approximation method underestimated the actual temperature in Celsius by 0.7 °C.

If you were looking only for quick methods to convert temperature values in Fahrenheit to Celsius and vice versa, this is all you need to know. You may skip the remaining post unless you want to know why these methods work.

## Analysis

In this section, we will see why these approximation methods work so well.

### Celsius to Fahrenheit Conversion

The method to convert temperature value from Celsius to Fahrenheit is equivalent to \[ \overset{\approx}{f} = 2 \left(c - \left\lfloor \frac{c}{10} \right\rfloor \right) + 31 \] where \( c \) is the temperature value in Celsius and \( \overset{\approx}{f} \) is the approximate temperature value in Fahrenheit.

Here is a brief justification for this:

- Subtracting one-tenth (floor division by 10) of \( c \) from itself gives us \( c - \left\lfloor \frac{c}{10} \right\rfloor. \)
- Doubling the previous result gives us \( 2 \left(c - \left\lfloor \frac{c}{10} \right\rfloor \right). \)
- Adding \( 31 \) to the previous result gives us \( 2 \left(c - \left\lfloor \frac{c}{10} \right\rfloor \right) + 31. \)

Now let us see how we arrive at the above approximate conversion formula. It's not too different from the exact conversion formula. The exact formula to convert temperature from Celsius to Fahrenheit is \[ f = \frac{9c}{5} + 32. \]

This can be rewritten as \[ f = 2 \left(c - \frac{c}{10} \right) + 32. \]

We don't want to deal with fractions, so we decide to approximate \( \frac{c}{10} \) in the above formula with \( \left\lfloor \frac{c}{10} \right\rfloor \) and get \[ \overset{\sim}{f} = 2 \left(c - \left\lfloor \frac{c}{10} \right\rfloor \right) + 32. \] where \( \overset{\sim}{f} \) is an approximation of the value in Fahrenheit. The floor division has the effect of potentially overestimating the final result by a value that is less than \( 2. \) This is the approximation error.

If we define the approximation error as \( \overset{\sim}{f} - f, \) then the approximation error lies in the half-open interval \( [0, 2). \) To ensure that the magnitude of the error never exceeds \( 1, \) i.e., to make the approximation error lie in the half-open interval \( [-1, 1), \) we subtract \( 1 \) from the above formula and get \[ \overset{\approx}{f} = 2 \left(c - \left\lfloor \frac{c}{10} \right\rfloor \right) + 31. \] This is the formula that the three-step method to convert temperature from Celsius to Fahrenheit is equivalent to.

### Fahrenheit to Celsius Conversion

The inverse method to convert temperature value from Fahrenheit to Celsius amounts to this formula: \[ \overset{\approx}{c} = \left\lfloor \frac{f - 31}{2} \right\rfloor + \left\lfloor \frac{f - 31}{20} \right\rfloor \] where \( f \) is the temperature value in Fahrenheit and \( \overset{\approx}{c} \) is the approximate temperature value in Celsius.

Here is a brief justification for this:

- Subtracting \( 31 \) from \( f \) gives us \( f - 31. \)
- Halving (floor division by \( 2 \)) the previous result gives us \( \left\lfloor \frac{f - 31}{2} \right\rfloor. \)
- Adding one-tenth (floor division by \( 10 \)) of the previous result to itself gives us \( \left\lfloor \frac{f - 31}{2} \right\rfloor + \left\lfloor \frac{1}{10} \left\lfloor \frac{f - 31}{2} \right\rfloor \right\rfloor = \left\lfloor \frac{f - 31}{2} \right\rfloor + \left\lfloor \frac{f - 31}{20} \right\rfloor. \)

This is roughly an inverse of all the steps for converting a temperature value from Celsius to Fahrenheit. Let us see if this is close to the exact conversion formula \[ c = \frac{5(f - 32)}{9}. \]

It turns out that it is in fact close to the exact conversion formula as follows: \begin{align*} c & = \frac{5(f - 32)}{9} \\ & = 0.5556 (f - 32) \\ & \approx 0.55 (f - 31) \\ & = \frac{11 (f - 31)}{20} \\ & = \frac{f - 31}{2} + \frac{f - 31}{20} \\ & \approx \left\lfloor \frac{f - 31}{2} \right\rfloor + \left\lfloor \frac{f - 31}{20} \right\rfloor = \overset{\approx}{c} \end{align*}

Like we discussed earlier, the magnitude of the approximation error does not exceed 1.89 °C for integer values of \( f \) between \( -148 \) and \( 212. \) The error here is a little bit more than the previous approximation method to convert temperature in Celsius to Fahrenheit but it is still small enough to give us a reasonably good estimate of what a temperature value in Fahrenheit would look like in Celsius if we are just talking about the weather.