I am currently learning about compiler construction and language design and I am thinking about what native datatypes I want to support in my language. Now there is a whole lot of languages that make a distinction between integer- and real numbers. However, I remember watching a talk by Douglas Crockford in which he said:

Having a single number type in the system means that you can not make a bug by choosing the wrong number type

He also mentioned that he recommends a number representation different to the commonly used IEEE-754 (please correct me if I am wrong), namingly being the DEC64. Hence my question: For a general-purpose language which has a primarily educational focus, what number representation should I use?

EDIT: With educational focus I am talking about my own progress in learning about compilers, not to educate others.

  • 4
    There is no right answer here. For educational purposes, I'd recommend using arbitrary precision decimals (e.g. Javas BigDecimal and BigInteger). Has all flexibility one can ask for, avoids having to explain rounding errors right at the start and behaves very much like the calculators pupils are used to. – marstato May 29 '18 at 6:46
  • 5
    The question starts asking about number types then veers to number representations. Which is kind of confusing, possibly also confused. Further, even opinion-based answers are likely to be off the mark if you can't clarify the educational focus you mention: is it to be a language for people learning to program (such as Pascal), a project for your own self-education, a language to be used by tots learning counting, ... ? – High Performance Mark May 29 '18 at 7:23
  • 1
    Thanks, I edited the question. I am mentioning both the number TYPES and REPRESENTATIONS because I firgured maybe someone could point me to the right direction for both of my problems :) – Niklas Vest May 29 '18 at 12:15
  • Just remember, being able to eliminate one class of bugs does not necessarily mean your net number of bugs goes down. – whatsisname May 31 '18 at 20:30
  • 1
    "which has a primarily educational focus" Like computer science educational? Because one issue you'll run into from a language design standpoint is supporting binary operators like arithmetic and logical shifts. (This plays into operator precedence grammar rules also if you include binary operators to learn how languages handle things). If you don't care about those then you could use a single data type. (JS has a single "Number" type if you want to see a weird way to handle things). – Sirisian May 31 '18 at 23:23

We have different number representation in general because they have different strengths and weaknesses, be it speed, precision, or range. Also this has to be the case because we cannot represent all Real numbers with finite memory, we always have to choose some that we cannot represent exactly.

The Doug Crockford quote you have is borderline idiotic, if you can only pick one representation then, OK you can't pick the wrong one, but you can't pick the right one either. i.e. your only choice will work for some uses but not for all.

It is true that some representations are probably better as first goto choice DEC64 looks reasonable here. It's a decimal floating point representation, so it will be less surprising than IEEE-754 (which is binary floating point) in most situations as people tend to think in decimal e.g. it can represent 0.3 exactly. It will still have representation issues in some circumstances e.g. adding really big and really small numbers together

for further reading I'd suggest Richard Harris' series of articles 'why X wont fix you flaoting point blues'

| improve this answer | |

For a general language, numbers should behave like those taught in math class. Only special-purpose languages, like those for device drivers, should have special mathematics.

I would recommend using arbitrary-precision numbers rather than fixed-precision ones. Yes, they're slower but they behave like people expect number to behave. Placing artificial limits on them will be reported as a bug.

| improve this answer | |
  • 5
    Unfortunately, numbers like those in math class cannot be represented in finite memory, which is the whole reason why we have so many different number representations in the first place. – Jörg W Mittag May 29 '18 at 15:53
  • 4
    And reporting "out of memory" when someone tries to calculate SQRT(2.0) will probably also be regarded as a bug. – Simon B May 29 '18 at 16:21
  • 3
    So, your single number type is in reality an infinity of number types depending on a few parameters. – Deduplicator May 31 '18 at 20:34
  • 2
    @shawnhcorey: So, you want to require manually specifying the precision for every single operation instead? – Deduplicator Jun 1 '18 at 0:59
  • 2
    @shawnhcorey Global state is evil. And now it interferes with all arithmetic? Also, how would two variables with different parameters interact? – Deduplicator Jun 1 '18 at 12:09

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.