## Figuring Odd or Even, Working with Fractions, and Calculating Quarts, Pints, and Cups

Last time in “Fake Math Tricks Using the Floor() and Mod() Functions (AutoHotkey Tips),” I introduced math techniques based upon the *Floor()* and *Mod()* functions. Although making basic calculations more complicated, many age-old units of measurement (e.g. yards, feet, and inches with fractions or gallons, quarts, pints, and cups with fractions) persist in common use among everyday endeavors. By obtaining the results from old-fashion division arithmetic showing quotients and remainders, we can write AutoHotkey routines for calculations in these varied types of units.

In order to provide more insight into how to use the Floor() function (quotient without remainder) and Mod() function (remainder or modulo), I offer a few examples of how to build calculators for these types of classic measurements.

### Odd or Even?

Depending upon your line of work, you may or may not need to determine the parity (odd or even) of an integer. As humans, we instantly recognize odd or even with a glance at the last digit (1, 3, 5, 7, 9 odd or 0, 2, 4, 6, 8 even). Computers must make a calculation—the easiest in AutoHotkey divides the number by 2 and looks at the remainder using the *Mod()* function:

```
test := 35
MsgBox % Mod(test, 2)=1 ? test " is odd!" : test " is even!"
```

The *Mod()* function returns the remainder after dividing the integer *test* by 2. If zero, the number is even. If one, odd.

Surprisingly, you may find this simple parity test useful for more applications than you might expect—including determining left and right pages in a book. For example, when converting partial inches worked out in decimals into fractions like those found on the standard American tape measure, the odd/even test saves a ton of code.

### Calculating Inch Fractions

I’m not sure how they do it in the rest of the world, but linear measurements in US construction commonly employ feet, inches, and fractions of an inch (e.g. 1/16, 1/8, 1/4, 1/2). Yet, when doing basic math on your calculator, you inevitably end up with decimals rather than the fractions of an inch that appear on your tape measure. The conversion of that trailing decimal to a fraction for an accuracy within 1/16 of an inch requires a special calculation:

```
inches := 12.345
test2 := Round(inches*16) ; convert to nearest 1/16
MsgBox % Mod(test2, 2) = 1
? Floor(test2/16) " " Mod(test2, 16) "/16"
: Mod(test2/2, 2) = 1
? Floor(test2/16) " " Floor(Mod(test2/2, 8)) "/8"
: Mod(test2/4, 2) = 1
? Floor(test2/16) " " Floor(Mod(test2/4, 4)) "/4"
: Mod(test2/8, 2) = 1
? Floor(test2/16) " " Floor(Mod(test2/8, 2)) "/2"
: Floor(test2/16)
```

AutoHotkey first converts the initial input into increments of 1/16 of an inch and rounds to the nearest whole unit.

`Round(inches*16)`

I’ve placed the entire calculation inside a MsgBox command using the ternary operator to embed a series of *If-Then-Else* expressions. The parity call checks for odd units at each level of the denominator. For example, the first parity check (`Mod(test2, 2) = 1`

) looks for an odd 1/16 of an inch—which cannot reduce to another fraction. If found, AutoHotkey displays the quotient (inches ➡ `Floor(test2/16)`

) and the remainder as sixteenths (fractions of an inch ➡ `Mod(test2, 16) "/16"`

).

If the first parity check finds no odd 1/16, then the operator looks for an odd 1/8 unit by dividing the original input by two (`Mod(test2/2, 2) = 1`

). If found, AutoHotkey displays the quotient (inches ➡ `Floor(test2/16)`

) and the remainder as eighths of an inch (fractions of an inch ➡ `Mod(test2/2, 8) "/8"`

).

When not found, the same *ternary* operator cascades into quarters (divide by four) and halves (divide by eight) before defaulting to a whole value of inches.

Tip: An industrious person could apply this to the MouseMeasure.ahk script for displaying fractions on blueprint images rather than the decimal equivalent on each measured leg.

### Using Only the Numbers to the Right of the Decimal Point

You could do this calculation with only the number to the right of the decimal point. However, by first converting the entire measurement to 1/16 of an inch increments, it leaves open the possibility of running other calculations (i.e. adding or subtracting measurements) prior to converting the answer to a fraction. If you want to start with the decimal form of the remainder, extract it with the following expression:

```
Mod(test3, 1)
```

The above examples do not show the complications added when measuring in feet plus inches. The techniques in the next section show how to get calculations done with similarly inconsistent units.

### Quarts, Pints, Cups, and Ounces

In the following example, you can use any set of classic units (e.g. stones, pounds, and ounces) as long as you adjust the values for each measurement. In this case, each quart contains two pints, each of which contains two cups, each of which contains eight ounces. By using the *Floor()* function to extract whole numbers of quarts (`Quarts := Floor(test/32)`

), the remainder allows for calculating, first, whole pints (`Floor(Mod(test, 32)/16)`

), then cups (`Floor(Mod(Mod(test, 32),16)/8)`

), and finally ounces (`Mod(Mod(Mod(test, 32),16),8)`

).

```
test := 350 ; ounces
Quarts := Floor(test/32)
Pints := Floor(Mod(test, 32)/16)
Cups := Floor(Mod(Mod(test, 32),16)/8)
Ounces := Mod(Mod(Mod(test, 32),16),8)
MsgBox %Quarts% Quart(s) `r %Pints% Pint `r %Cups% Cup `r %Ounces% Ounce(s)
```

Note how each step of the calculation uses the remainder (*Mod()* function) from the previous unit to extract the smaller unit. You could use this technique to add or subtract mixed units by first converting everything to a common increment (in this case ounces), make the calculation, then use the above logic to convert back to mixed units.

*Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)*

*This post was proofread by **Grammarly*

(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!