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:
Continue reading →
test := 35
MsgBox % Mod(test, 2)=1 ? test " is odd!" : test " is even!"