Question

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.

Was it helpful?

Solution

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'

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
scroll top