shopspring / decimal

Arbitrary-precision fixed-point decimal numbers in Go
Other
6.34k stars 621 forks source link

Context Support #206

Open AncientHodler opened 3 years ago

AncientHodler commented 3 years ago

I wanted to ask if this library has something like a suport for a context. A constext would be somethin like, setting a certain precision, certain round up for,..

Like i want all my math to be computed to the 250th decimal and no more. If i set the div precision to 250, doing multiple division would yield larger decimal number, and further doing math on those numbers would become increasingly time consuming.

My solution now would be to truncate all results to my give precision, or truncate the division function to 250 precision.

Or i could implement the context feature myself, but i dont know for now how to handle errors.... need to look that up.

The easiest way would be to truncate the math operations to a precision higher 1 or two points less than the division precision.

Im asking because the Decimal implementation in python has the context mechanic, and the Cockroach apd decimal library also has it .... ill probably going to look there to see how they done it

mwoss commented 3 years ago

Nope, right now this library only operates on global variables such as DivisionPrecision and MarshalJSONWithoutQuotes. I was thinking about adding support for context, but it has to wait for the 2.0 release as it would introduce a breaking change. There are many things that should have done differently, but doing a breaking changes after 1.0 release is not something advisable.

AncientHodler commented 3 years ago

why would it make a breaking change ? anyway i was thinking on modifying the basic math functions to truncate to a certain decimal precision i want and need. The thing with the context precision in cockroachdb is that a 250 precision is the sum of the numbers before and after the decimal point. So if the number is 10 million which is 8 numbers big, your left with only "242" decimal precision....

Been trying yesterday for 9 hours straight to use the cockroach db, but their math syntax is somehow combersome, and couldnt make the sinus function work at all with it. as opposed to you library which implemented it correctly. So im gonna stick with you library and truncate my math operations.

They use a different structure for the decimal type, type Decimal struct { Form Form Negative bool Exponent int32 Coeff big.Int }

and their function uses the *Decimal type, as oposed to your using the Decimal type. Cant really understand the difference (im new to golang) but it seems to function a bit ackward... Go says their two different thing ...

gonna have to write a function for logarithm though as there isnt any ....

mwoss commented 3 years ago

Right now I can't think of a clean way to introducing context support for a decimal lib without not changing any of the existing method signatures. I would have to introduce a new type with all math operations redefined, pass context var as a parameter to methods, or just add it as a decimal struct field. It could be also stored as global var (something to what Python implementation of the decimal is doing), but I have concerns it won't be the best idea to move on with. I would have to rethink that.

nicktobey commented 5 months ago

Being able to controlMarshalJSONWithoutQuotes via some kind of context would be incredibly useful. Only using global variables can cause huge problems in multi-threaded code, or if multiple libraries depend on this module.