4
$\begingroup$

I would like to be able to enter numbers and have them interpreted in base 12 and output in base 12. For example, I want

13+A

to produce

21

Of course, Mathematica allows the use of other bases:

BaseForm[12^^13 + 12^^A, 12]

returns

21_12

but I would rather not have to enter the extra stuff.

I imagine I would need to alter my environment similar to below. I don't mind breaking things. I just want to be able to do simple arithmetic in base 12.

$\endgroup$
2
$\begingroup$

Updated to work with strings instead of boxes

One idea is to create a "Duodecimal" style that automatically interprets strings as base 12 integers, and outputs the result in BaseForm. This is an example stylesheet that does this:

SetOptions[
    EvaluationNotebook[],
    StyleDefinitions -> Notebook[{
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["Duodecimal",StyleDefinitions->StyleData["Input"]],
            CellEvaluationFunction->Function[
                BaseForm[
                    With[
                        {
                        g = StringReplace[
                            #,
                            {
                                StartOfString~~i:DigitCharacter~~m:{DigitCharacter,"A","B"}..~~r___:>"12^^"<>i<>m<>" "<>r,
                                StartOfString~~m:{DigitCharacter,"A","B"}..~~EndOfString:>"12^^"<>m
                            }
                        ]&
                        },

                        ToExpression @ StringReplace[
                            #,
                            {
                            f:(DigitCharacter..~~"."~~DigitCharacter...):>f,
                            l:WordCharacter.. :> g[l]
                            }
                        ]
                    ],
                    12
                ]
            ],
            DefaultFormatType->"RawInputForm"
        ]
    },
    StyleDefinitions->"PrivateStylesheetFormatting.nb"]
]

Note that I use DefaultFormatType->"RawInputForm" so that there is no space between the 1 and the A in an input like "1A". Both InputForm and StandardForm will display an input like 1A with a space.

Using "RawInputForm" means that the input is a simple string (as you can tell by using Show Expression) from the Cell menu. I originally tried to convert the string to boxes, and then processed the boxes so that the input was in base 12. However, this didn't quite work (there were issues with both multiplication and powers), so now I instead process the input string. I avoided processing real numbers (as indicated by a .) and I converted words to a base 12 string if the word started with an integer or the whole word consisted strictly of duodecimal characters. Finally, I included a BaseForm wrapper in the output. You can always eliminate this and use your $Post method instead.

To use this style, enter Alt-0 and type in Duodecimal. If desired, one could also give it a MenuCommandKey, or add a StyleKeyMapping to the "Input" style.

Here is a screen shot of a couple examples:

enter image description here

$\endgroup$
0
$\begingroup$

For the output side of things the following seems to work:

$PrePrint = Replace[n_Integer :> IntegerString[n, 12]]

Then

12^^13 + 12^^A

produces

21

I don't know whether something similar can be done with $PreRead. I don't mind losing my strings starting with a and b.

$\endgroup$
  • $\begingroup$ The problem with $PreRead is that it is already after tokenization stage and when you type 2-F-(Ctrl+6)-2 and expect (2F)^2, you won't get it. $\endgroup$ – Kuba May 9 '17 at 11:41
  • $\begingroup$ F? Hexadecimal? Do you mean 2F^2? $\endgroup$ – pdmclean May 9 '17 at 11:46
  • $\begingroup$ Sorry, meant A or B, doesn't matter what base. And I meant to show short form of (12^^2A)^12^^2 $\endgroup$ – Kuba May 9 '17 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.