Decimal pure zig

hello , new decimal 34 digits pure zig

zig_decimal

A rewritten fixed decimal management library, for business

management, I abandoned “mpdecimal” since my discovery of f128 :

// autre mode de calcul avec une evaluation d'expression
stdout.print("\n test eval-expression  result:f128 \n",.{}) catch {};
const E_benefice = dcml.Expr{
        .Sub = .{
            .left = &dcml.Expr{
                .Sub = .{
                    .left = &dcml.Expr{ .Val = article.montVente.val },
                    .right = &dcml.Expr{ .Val = article.prixAchat.val },
                },
            },
            .right = &dcml.Expr{
                .Add = .{
                    .left = &dcml.Expr{ .Val = article.maintenance.val },
                    .right = &dcml.Expr{
                        .Add =.{
                            .left = &dcml.Expr{ .Val = article.salairs.val },
                            .right = &dcml.Expr{ .Val = article.impots.val },
                        },
                    },
                },
            },
        },    
    };

    try dcml.show(&E_benefice, &stdout);
    try stdout.print(" = {d}\n", .{dcml.eval(&E_benefice)});

    article.benefice.set(dcml.eval(&E_benefice));
    article.benefice.round();
    stdout.print("benefice.{s}\n",.{article.benefice.string()}) catch {};

34 digits all the usual functions plus an evaluation function .

2 Likes

Hello, I’ve introduced an edit-code notion for representations, for example accounting, and lots of little tricks.
I’ve finished the 100% SQL-compatible string output

Since you are an AS400-type person, how does this Decimal correspond with the stuff from Mike Cowlishaw:

If you want to use this, you have to use mpdecimal.
Which I gave up, and went back to f128 with 35 characters, I limited it to 34, otherwise I used the same methods.
On the AS400, don’t forget that operations (add sub mult div rem) are designed for business management, there are standards. DB2 lends itself very well.
So zones are bounded ex: numeric(12,3) 12 integer 3 decimal
Zig made a very clean float 128.
You can do calculations and you can decide when to round or truncate.
in string() output I normalize so I truncate if you want to be right after calculations, in some cases it is better to round off before doing string().

PostgreSQL has had this function in place for some time, with zones defined at NUMERIC
Why I abandoned mpdecimal, or I could have worked with decNumber .zip from speleotrove which I’ve already done in Nime or C++.
But I’m dependent on an external lib in that case.
And when I saw that f128 was released, I looked into it. The hardest part isn’t the calculation, it’s the bounding and getting it right.

I could put back in the git the module that works with mpdecimal if you’re really interested

Another reason for the NUMERIC zone is that we’re based on production where the machines have specifications, and we have to apply machine modeling, so those who are going to use the software need precise correlations.
In accounting or sales, NUMERIC zones are essential for both visualization and processing, and especially for tax purposes, where you need to be able to demonstrate reliability and legal compliance.

Another reason is that I work on a PC and I use a language and I have to communicate with databases, which I have to read and bring a code bar reading that will impact the stock, if I can’t validate a conformity linked to the definition of the central server, in this case an AS400 so DB2, I have to know how to work with fields of the same type (attribute).

Mike Cowlishaw contributed a lot to this format, but he didn’t have any restrictions, at least when it came to making it available as open-source. (not IBM400)

almost 20 years ago

1 Like