til-lang / til

An easy to extend command language
56 stars 5 forks source link

Decimal floating point by default (instead of binary floating point) #3

Open dumblob opened 3 years ago

dumblob commented 3 years ago

C23 (implementations of proposals finished in 2021 and formally adopted in 2022 as planned) will natively support _Decimal32 _Decimal64 and _Decimal128.

This is one of the biggest changes C had since C99. Presumably it'll create an enormous pressure on all other languages to support decimal floating point as basic number type.

Til could reconsider whether making all float literals (and all floating point arithmetics) a decimal floating point number wouldn't be the easier way to go. Otherwise if it won't be part of Til at that time, Til will have a hard time to catch up unless breaking backwards compatibility will not be an issue. Such a change takes years which is really no good for a new language.

I'd recommend using mpdecimal library (CPython uses it as well) if decimal floats shall become part of Til.

Note also I personally don't think there is any need for making floats BigDecimal by default.

cleberzavadniak commented 3 years ago

Yeap, another item in my not-yet-written list of things to consider. Floating point in general is basically untouched.

For now the thing is that most desktop chips are very, very good at handling floating point arithmetics. And you can use SIMD instructions on them, too.

So, IDK... maybe I'll follow the "delegational" model: if your module wants to work with decimals, it has access to the original representation as a string and can make conversions as needed. The same for 4-byte floats or any other format. (But I would like to know how this decision would impact someone who is really going to do some kind of scientific work using Til.)

dumblob commented 3 years ago

Well, actually the underlying library Python uses for Decimal is mpdecimal and has only about 2x longer computational times in expressions than using native binary floats (doubles in C terms) - but this measurement was IIRC without binary floats using SIMD (because they were all doubles and thus SIMD wouldn't have the gains we're expecting - SIMD is IMHO mostly used for single-precision binary floats). That's actually very good considering what decimal floats offer.

So I'd say just use D's decimal module (or throw in mpdecimal) as the default float number and don't think about it any more. Most people wouldn't notice except suddenly things like 1.1 + 1.2 would work as expected. The other possibility is to write out in "compile time" (or parse time) an error if the given number literal is not losslessly convertible to the underlying binary float format (e.g. for 1.1 the compiler would error out with message 1.1 is not supported - use 1.10000002384185791015625 insted).

Scientific libraries could get about 2x slower if they were written exclusively in Til (which I have no idea why anyone would want to do if there'll be compile-time FFI or modules written in D).