PREP 2014 Question Authoring - Archived

Allowing a comma instead of a dot for a decimal point (and other notation variants)

Re: Allowing a comma instead of a dot for a decimal point (and other notation variants)

by Davide Cervone -
Number of replies: 0
Paul, this looks good, except for two details. The first is that the pattern for number is not right. You have left too much of the one from the Wiki, so it allows commas every three characters. You want
    Context()->{pattern}{number} = '(?:\d+(?:[.,]\d*)?|[.,]\d+)(?:E[-+]?\d+)?';
Note that this does allow ,123 as a means of entering 0.123, which looks strange to my eye. I don't know if that is the usual notation or not. If you want to force a zero before the comma, use
    Context()->{pattern}{number} = '\d+(?:[.,]\d*)?(?:E[-+]?\d+)?';
    Context()->{pattern}{signedNumber} = '[-+]\d+(?:[.,]\d*)?(?:E[-+]?\d+)?';

The second issue has to do with the text of the problem, not the answer checking. In TeX, the comma is formatted with a little space after it (it has the TeX class of punctuation, and the extra space makes things like vectors format better). When used as a decimal indicator, it should not have that space. To avoid that, use braces around the comma. So the statement of the problem should be

    BEGIN_TEXT
    Enter \( 3.14 \) or \( 3{,}14 \): \{ ans_rule(10) \}
    END_TEXT
This is a TeX subtlety that is easily missed, but if you look at the spacing for these, you will see a difference.