Fake Math Tricks Using the Floor() and Mod() Functions (AutoHotkey Tips)

Although Not Really Fake Math, These Two AutoHotkey Functions Can Solve Eclectic, Yet Practical, Everyday Problems!

The Irish philosopher George Berkeley proved that mathematics does not exist in reality. It turns out nobody cares! Even though math resides only in our heads, we continue to use it anyway.

Berkeley did not doubt that calculus produced real-world truth; simple physics experiments could verify that Newton’s method did what it claimed to do. “The cause of Fluxions cannot be defended by reason”,[48] but the results could be defended by empirical observation, Berkeley’s preferred method of acquiring knowledge at any rate. Berkeley, however, found it paradoxical that “Mathematicians should deduce true Propositions from false Principles, be right in Conclusion, and yet err in the Premises.” In The Analyst, he endeavoured to show “how Error may bring forth Truth, though it cannot bring forth Science”.[49] Newton’s science, therefore, could not on purely scientific grounds justify its conclusions, and the mechanical, deistic model of the universe could not be rationally justified.[50]

https://en.wikipedia.org/wiki/George_Berkeley#Philosophy_of_mathematics

Fortunately, we don’t need to agree with (or even understand) the above citation to take advantage of the results from mental calculation. Scientists may show concern about the theoretical but engineers only care about what works.

Stupid Units of Measurements

Throughout history, Man has shown a capacity for creating incomprehensible methods for measuring things without regard for the pain these concepts inflict on future generations. The calendar merely represents one example. What about gallons, quarts, pints, and cups (not to mention the pretentious—yet satisfying—Imperial pint)? Who invented yards, feet, and inches (with fractions)? Comparing the totally-arbitrary Fahrenheit scale with the slightly-less-arbitrary Celsius scale offers mental gymnastics worthy of all the great mathematicians. While kilos and grams make a little more sense than pounds and ounces, I still hear an occasional disparaging remark about someone who weighs 14 stones—although not in the United States. Don’t get me started on the old British currency system which, unlike my wife, I never did comprehend.

Most of the rest of the world (and the scientific community) have abandoned these systems in favor of much more boring decimal systems—but not the United States. We cling to our traditional notions about life, the universe, and everything. This presents us with tricky problems. Fortunately, AutoHotkey includes two functions for dealing with these insidious situations: Floor() and Mod().

In this blog, I demonstrate how we can eliminate the loops in last week’s DateCalc() function with a combination of the Floor() and Mod() functions. It works by using old-fashioned (non-decimal) arithmetic long division.

Old-Fashioned Arithmetic Long Division

Image source: Wizant.com

In this day of electronic calculators, we convert everything to decimals. But that’s not how I first learned to do division.

In fourth grade, I carefully worked on a long division problem by first determining the number of times one number goes into another, then I called the leftovers the remainder or modulo (shown in red in the image at right). Doing this type of math makes it possible to perform the month’s calculation problem presented in last week’s blog “Calculating Dates in AutoHotkey by Adding Years, Months, and/or Days” without using any loops.

By employing the Floor() function for the basic division problem and the Mod() function to determine the remainder, I get a direct result—eliminating the need for incrementing loops that add or subtract one year to or from the years variable and 12 months to or from the months variable. AutoHotkey now makes the calculation directly.

Calculating Years and Month in One Fell Swoop

The Floor() Function

The Floor() function rounds any number down to the nearest integer. This effectively removes any remainder from consideration in a division expression (e.g. var/12) used as the function parameter.

For the DateCalc.ahk problem, the Floor() function calculates the total number of years available in the months variable by dividing that number by 12 and rounding down to the nearest integer:

CalcYears := Floor(Months/12) + Years

This operation truncates any fraction to the right of the decimal point.

The Mod() Function

The Mod() function specifically returns the remainder from any long division problem. The Mod() function pulls out the remaining number of months after subtracting the quotient for that same long division problem:

   CalcMonths := Mod(Months,12)

Since the calendar does not offer zero or negative numbered months, the only additional code required corrects for a zero or negative value of CalcMonths when subtracting months to find a new date:

	Months := SubStr(Date,5,2)+Months
    CalcYears := Floor(Months/12) + Years
    CalcMonths := Mod(Months,12)
	If (CalcMonths <= 0)
	{
		CalcYears := CalcMonths = 0 ? CalcYears-1 : CalcYears
		CalcMonths := CalcMonths + 12
	}
	NewDate := Substr(Date,1,4)+CalcYears . Format("{:02}", CalcMonths) . Substr(Date,7,2)
	NewDate += Days , Days

Whenever the computed value of CalcMonths (lines 4 through 8) falls below 1, AutoHotkey adds 12 to that value to produce the correct new month number. For a 0 value, AutoHotkey must both add the 12 months to the CalcMonths variable and subtract 1 year from the CalcYears variable. (A CalcMonths value of 0 represents December of the previous year.)

To make the change to last week’s DateCalc.ahk script, replace the following two-loop routine with the above Floor() and Mod() code:

	Months := SubStr(Date,5,2)+Months
	While (Months > 12)
	{
		Years++
		Months := Months - 12
	}
	While (Months <= 0)
	{
		Years--
		Months := Months + 12
	}
	NewDate := Substr(Date,1,4)+Years . Format("{:02}", Months) . Substr(Date,7,2)
	NewDate += Days , Days

The new version of the DateCalc.ahk script should appear as follows:

StartDate := "20200913"
Years := -2
Months := -8
Days := 0

NewDate := DateCalc(StartDate,Years,Months,Days)

MsgBox % SubStr(NewDate,1,8)

DateCalc(Date := "",Years := 0,Months := 0,Days := 0)
{
	If (Date = "")
		Date := A_Now
	Months := SubStr(Date,5,2)+Months
    CalcYears := Floor(Months/12) + Years
    CalcMonths := Mod(Months,12)
	If (CalcMonths <= 0)
	{
		CalcYears := CalcMonths = 0 ? CalcYears-1 : CalcYears
		CalcMonths := CalcMonths + 12
	}
	NewDate := Substr(Date,1,4)+CalcYears . Format("{:02}", CalcMonths) . Substr(Date,7,2)
	NewDate += Days , Days
	Return NewDate
}

As always, if you find any bugs, please let me know.

Next week, I plan to offer a series of other practical applications for the Floor()/Mod() long division model including determining odd and even numbers, doing fractions, and calculating the number of quarts, pints, cups, and ounces in any given volume.

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.)

jack

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“!

One thought on “Fake Math Tricks Using the Floor() and Mod() Functions (AutoHotkey Tips)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s