Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: decimal floating-point types #4221

Open
SaintCharlesCode opened this issue Jan 18, 2020 · 9 comments
Open

Proposal: decimal floating-point types #4221

SaintCharlesCode opened this issue Jan 18, 2020 · 9 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@SaintCharlesCode
Copy link

What are your thoughts on Zig having decimal floating-point types built into the language? http://speleotrove.com/decimal/
They could have types df32 df64 df128 or something like that.

@JesseRMeyer
Copy link

There's also the general possibility of user-space defined Fixed Point storage.

@shawnl
Copy link
Contributor

shawnl commented Jan 18, 2020

you should probably bring this up with llvm. This is a bunch of work (it needs a full soft float library) and that work would probably belong in llvm.

@SaintCharlesCode
Copy link
Author

That's true. Also clang does not have "(_Decimal32 and friends)" according to the manual https://clang.llvm.org/docs/UsersManual.html It is probably too much of a niche feature.

@daurnimator daurnimator added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jan 18, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Jan 28, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@DanB91
Copy link

DanB91 commented Mar 26, 2021

Came across this article and adding this in could maybe help in places where COBOL is used: https://medium.com/the-technical-archaeologist/is-cobol-holding-you-hostage-with-math-5498c0eb428b

@matu3ba
Copy link
Contributor

matu3ba commented Apr 22, 2021

This is identical to the more poorly named #1974.
Look especially at this nice comment, which elaborates quite alot: #1974 (comment)

Though I disagree that a separate type should be reserved for it, since the type consists of a tuple (and it could be also done in libstd). There may be eventually some news on this, since another project lowers a functional language to zig and uses something in zig for that.

@ghost
Copy link

ghost commented Apr 22, 2021

At the moment, #1974 is only about binary fixed-point numbers, so it wouldn't help with this issue.

@matu3ba
Copy link
Contributor

matu3ba commented Apr 23, 2021

At the moment, #1974 is only about binary fixed-point numbers, so it wouldn't help with this issue.

They are very closely related: https://www.tutorialspoint.com/fixed-point-and-floating-point-number-representations,
so I dont see a special reason to separate them.
Since we have comptime, they could probably even be done together.

If you want to be fancy you could also use binary-coded decimals, thought thats less efficient (but might be useful for transformation etc).

Its quite a pity that wikipedia does not properly explain it.

@ghost
Copy link

ghost commented Apr 23, 2021

@matu3ba,
Fixed vs floating point and binary vs decimal are orthogonal problems, as I understand them. The former trades range for uniformity of representable numbers (and sometimes also performance). The latter determines which fractional numbers are representable exactly and which aren't. E.g., 0.1 is not representable as a binary fraction, regardless of whether it is fixed or floating. Although the rounding errors are usually small, they sometimes do matter in financial and accounting software. And they can compound quickly, with the classic example being:

var x = 0.01; // type omitted
var i: i32 = 0;
while (i < 12) : (i += 1) {
    x = 1.0 - 99 * x;
}

At the end of the 12 iterations, x would have the value 0.01 if it is represented as a decimal fraction. But with a binary fraction, each step eats about 6 bits of precision. If x: f64, then the result is -79522. This is the kind of thing people are usually trying to prevent when calling for decimal number support.

To be clear, I'm highly skeptical of introducing native decimal fraction support in Zig. I'm just saying that if we do, then #1974 is not sufficient, unless it is extended to specify the base as well.

@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@haberman
Copy link

It looks like decimal floating point is an optional feature in C23: https://www.iso.org/standard/68882.html

GCC seems to have some amount of support for it: https://gcc.gnu.org/onlinedocs/gcc/Decimal-Float.html

Example usage: https://godbolt.org/z/d6EzqcrE6

There is talk of implementing this in Clang, but nothing tangible yet AFAICS: https://discourse.llvm.org/t/rfc-decimal-floating-point-support-iso-iec-ts-18661-2-and-c23/62152/1

@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

8 participants