Hacker News new | ask | show | jobs
by insertion 3865 days ago
I would like to see a language use decimal by default. Under the hood, it could use floating point for certain types of calculations where less precision is required. If I want to know the answer to 0.2 + 0.1, chances are I want the answer to be 0.3. If I'm writing performance-sensitive code, maybe I can drop down a level and opt for float(0.2) + float(0.1). Are any languages doing this currently?
4 comments

Perl 6 uses rationals by default. They have the advantages of being base-agnostic, able to accurately represent any recurring digit expansion accurately regardless of eventual base, and also faster (since, especially if you normalize (convert to lowest terms) lazily, most operations are just a few integer instructions with no branching, looping, or bit-twiddling involved).
True. Also notice that if the user wants a floating point, the literal format is the one with the e<exponent> suffix:

    0.1e0, 1e-1, 0.2e0, 2e-1 etc.
You want COBOL. It did decimal by default 50 years ago.
Wouldn't you get other issues with decimal numbers? Consider 1/3 for instance.

What you really want, I think, is rationals by default, as in Perl 6.

> I would like to see a language use decimal by default.

So, Scheme?

Scheme has rationals, but decimal literals aren't rational by default. You have to type #e in front of it, or express it as a fraction.

It really ought to be fixed at the implementation level (how much stuff would it really break if floatin point errors went away?), but failing that I'd love a macro that did it for me. I can't figure out how to write one robustly.