Mixed Radix Conversions

Created by Dr. Irv Bromberg, University of Toronto, Canada email icon

[Click here to go back to the Symmetry454 / Kalendis home page]

Join the Group.io logo Calendars LISTSERV


Introduction to Mixed Radix Conversions

In standard numeric positional notation the same base is used for all positions, for example in base 10 (decimal) notation the positions have these values:

106 105 104 103 102 101 100 . 10-1 10-2 10-3 10-4, and so on as necessary in both directions from the decimal point.

In hexadecimal, the positions have these values:

166 165 164 163 162 161 160 . 16-1 16-2 16-3 16-4, and so on as necessary in both directions from the hexadecimal point.

In binary, the positions have these values:

26 25 24 23 22 21 20 . 2-1 2-2 2-3 2-4, and so on as necessary in both directions from the binary point.

By contrast, in mixed radix number systems the bases used may vary between positions, but each base is a multiple of the next lower base, and each base is a positive integer equal to 2 or more. Here are some typical applications:

Chapter 1, page 27, section 1.10 of the book Calendrical Calculations: The Ultimate Edition (hereinafter abbreviated as CCUE), by Edward M. Reingold and Nachum Dershowitz, published by Cambridge University Press in 2018, introduced the arithmetic of mixed radix conversions, defining to-radix( ), from-radix( ), sigma( ) and round( ) functions in their corresponding LISP program code, and giving very limited examples of their use. Later in the book the authors applied mixed radix conversions to alternative Gregorian calendar arithmetic, as well as to limited use in the Icelandic, Hebrew, and Mayan calendars.

Because the CCUE description of mixed radix conversions was so terse and included few examples, I found it very hard to understand, and so set about learning it by implementing a set of functions to experiment with. I selected the Visual Basic for Applications (VBA) computer programming language (open source) in the Microsoft Office Excel developer environment.

Click here to download the Excel workbook Mixed-Radix-conversions.xlsm 239 KB

The workbook file is in Microsoft Excel’s macro-enabled XML file format (.xlsm extension). A macro is computer code written in Excel’s Visual Basic for Applications (VBA) programming language. Depending on your security settings, Excel may prompt you to allow enabling of macro execution, which is necessary for the examples to work. XML is the native file format for Excel 2007 and later. I tried saving the workbook in the older XLS format, but the file size was about 3 times larger. I have tested this workbook in Excel 2003 and 2007 under 32-bit Windows XP, in Excel 2013 and 2016 under 64-bit Windows 10, Excel for macOS 2011 version 14.7.7 under the last version of macOS High Sierra, Excel for Mac 16.57 under macOS Monterey 12.2, LibreOffice Calc 6.0.7.3 under Linux Mint Uma, and x64 LibreOffice Calc 7.2.5 under Windows 10. To open the file in versions of Excel prior to 2007 you need the XML file converter plug-in from Microsoft — failing that, contact me to make the older XLS version available. The steps required to enable VBA macro execution differs in these various environments — search the internet for instructions.

LibreOffice Calc natively opens the .xlsm file, but for the examples to work you need to enable execution of macros once only as follows: from the Tools menu choose Options..., then under LibreOffice...Security click on the Macro Security... button, then choose the Medium option (in which case upon each file open Calc will ask the user for permission to enable macro execution) — the default is probably High or more, which won’t allow execution of macros, and won’t ask. If you make changes in Calc then use the Save As command to save the file in Open Document Spreadsheet format (.ods), and there will be no going back to Excel. Although I’ve only tested LibreOffice Calc under Linux and Windows, I expect it to work with this spreadsheet on any supported platform. An annoyance is that Calc shows all VBA function names in UPPERCASE in worksheet cell formulas. The similar program, Apache OpenOffice Calc, might work as well — if you have OpenOffice then please test it and let me know. If CALC asks This file contains links to other files. Should they be updated? then click the No button, because there aren’t any external links.

The workbook include a few examples where Hebrew units are applied to radix values. Excel for Windows or Mac displays these properly, but LibreOffice Calc puts the highest radix value leftmost (instead of rightmost) and the highest radix unit rightmost. The lesser radix values and units do display properly in Calc.

The first time that you open the workbook in Excel for Mac, you might see #NAME! in the VBA-calculated cells for several minutes (during this time you may notice the Visual Basic editor window flickering), then they will all display properly. After that, save the workbook, and from then on when you open the workbook all cells will display promptly. There is an anomaly in Excel for Mac: the ApplyUnits( ) function uses a Windows non-breaking space = Chr$(160) as delimiter between each radix value and its unit, but Excel for Mac displays this as an annoying dagger symbol (†). The beginning of the VBA code offers the mid-dot or raised dot as an alternative = Chr$(183), but Excel for Mac displays that as a Greek uppercase sigma character (Σ). Until a better alternative is found [a normal space = Chr$(32) won’t work properly], you might prefer to set Const DelimiterCode As Integer = 95 (underscore _) near the top of the VBA module — the first recalculations after changing that may take several minutes, during which Excel may be unresponsive and its windows may flicker annoyingly.

How do these VBA functions differ from the CCUE functions?

I found that the CCUE algorithms are rather inefficient, re-using the listed bases many more times than is strictly necessary, and using recursion and passing of lists to implement the to-radix( ) function. In LISP it is natively simple to pass growing lists between functions, but I don’t know of way to do this in Excel VBA. In the VBA FromRadix( ) function, I found that if execution always processes the radix values from the radix point to the left and then from the radix point to the right then each base need only be accessed once, with the accumulating multiplier or divider carried forward to the next radix value. The CCUE lists of radix values always omit the radix point (semicolon), but I found that the correctly positioned radix point is mandatory for unambiguous conversion of a list of radix values back to a number, in particular if leading and/or trailing zeros were trimmed from the list of radix values.

Worksheets in the Workbook

The Syntax worksheet details the function parameters and how to invoke the functions.

The Calendar worksheet focuses on examples related to calendars, cycles, mean year or month, time, angles, and so on. Despite the easily encountered limitations of double precision floating point calculations in VBA, I found that mixed radix conversions are especially useful for converting a number with decimal points to the exact time duration of a solar or lunisolar calendar mean year, or lunar cycle mean month. The version of Excel that I used for some of this work was the 64-bit 2016 release in Windows 10, which supports the new 64-bit LongLong integer type (compare with the 32-bit Long integer type) that might have been useful in cases where floating point imprecision let me down, but then the least radix value couldn’t contain decimal points and users of the 32-bit versions of Excel would be unable to use this work.

The Mixed worksheet contains general examples of mixed radix conversions.

The Single worksheet contains examples of interconversions using a single base.

The Web worksheet contains titles and URLs of world wide web resources concerning mixed radix conversions.

Please Provide Feedback for this Project

I would really appreciate receiving comments, bug reports, and suggestions about this work, as well as further examples to add to the demo collection and useful URLs to add to the Web worksheet in the Excel workbook.


Updated 5 Adar Sheini (Traditional) = 5 Adar Sheini (Rectified) = Mar 2, 2022 (Symmetry454) = Mar 4, 2022 (Symmetry010) = Mar 8, 2022 (Gregorian)