Category talk:Wren-big: Difference between revisions

→‎Arbitrary precision arithmetic: Change to blurb following addition of BigDec.
(→‎Source code: Added BigDec and BigDecs classes.)
(→‎Arbitrary precision arithmetic: Change to blurb following addition of BigDec.)
Line 5:
Wren is unable to deal natively and precisely with integers outside a 53-bit range and so either has to limit such tasks to integers which it can deal with or is unable to offer a realistic solution at all.
 
This module aims to remedy that situation by providing classes for arbitrary precision arithmetic (integers, rational and rationaldecimal numbers) in an easy to use form. It is based on or guided by Peter Olson's public domain [https://github.com/peterolson/BigInteger.js BigInteger.js] and [https://github.com/peterolson/BigRational.js BigRational.js] libraries for Javascript, a language which until the recent addition of native BigInt support was in the same boat as Wren regarding the 53-bit limitation.
 
;BigInt class
Line 29:
There are also separate methods to enable you to to generate a BigRat from a Rat object, from a string in rational form (i.e. "n/d"), from a mixed string (e.g. "1_2/3") from a decimal string or value (e.g. "1.234") or from a Num.
 
;BigDec class
As rational numbers can easily be created from or displayed as decimal numbers, there seems little point in adding a BigDecimal class to the module as well other than to limit precision. However, the ''round(digits)'' method can be used for this purpose when performing a series of calculations.
 
As rational numbers can easily be created from or displayed as decimal numbers, at first glance there seems little point in adding a BigDecimalBigDec class to the module as well other than to limit precision. However, the ''round(digits)'' method can be used for this purpose when performing a series of calculations.
 
However, a problem with BigRat is that it always works to maximum precision which uses a lot of memory and can be very slow in certain circumstances.
 
BigDec aims to fix this problem by working to a user defined precision, where precision is measured in digits after the decimal point. One can therefore limit precision having regard to the accuracy needed for a particular task though, to dove-tail with the maximum accuracy of ordinary Nums, a minimum (and default) precision of 16 is imposed.
 
Otherwise, BigDec is simply a thin wrapper over BigRat and defers to it for the implementation of most of its methods. Where all you need is decimal numbers, it is also somewhat easier to use than BigRat as the user never has to deal with the latter directly.
 
;General observations
 
The original Javascript libraries and this module are easy to use as they try to mimic native arithmetic by always producing immutable objects and by implicitly converting suitable operands into BigInts, BigRats or BigRatsBigDecs. This is accentuated further by Wren's support for operator overloading which means that all the familiar operations on numbers can be used.
 
Of course, such a system inevitably keeps the garbage collector busy and is not going to be as fast and efficient as systems where BigInts, BigRats or BigRatsBigDecs are mutable and where memory usage can therefore be managed by the user. On the other hand the latter systems can be difficult or unpleasant to use and it is therefore felt that the present approach is a good fit for Wren where ease of use is paramount.
 
===Source code===
9,476

edits