Category talk:Wren-big: Difference between revisions
→Arbitrary precision arithmetic: Extended preamble to include the new BigRat class.
(→Source code: Fixed translation error.) |
(→Arbitrary precision arithmetic: Extended preamble to include the new BigRat class.) |
||
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 (
;BigInt class
Some changes or simplifications have been made to fit Wren's ethos more closely. In particular:
Line 13 ⟶ 15:
:* There is no support for bases below 2 or (as far as the public interface is concerned) above 36 as practical applications for such bases are very limited. Similarly, support for user-defined digit alphabets has also been excluded.
:* As currently written, this
The ''BigInt.new'' constructor accepts either a string or a ''safe'' integer (i.e. within the normal 53-bit limitation). I couldn't see much point in accepting larger numbers (or floats) as this would inevitably mean that the BigInt object would be imprecise from the outset. Passing an empty string will also produce an error (rather than a zero object) as it's just as easy to pass 0 and an empty string may well be a mistake in any case.
Line 19 ⟶ 21:
If you need to generate a BigInt from a string in a non-decimal base or from another BigInt, you can use the ''fromBaseString'' or ''copy'' methods, respectively.
;BigRat class
The original Javascript library 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. This is accentuated further by Wren's support for operator overloading which means that all the familiar operations on numbers can be used.▼
This follows the lines of the Rat class in the [https://rosettacode.org/wiki/Category:Wren-rat Wren-rat] module though I have been guided by [https://github.com/peterolson/BigRational.js BigRational.js] when coding some of the trickier parts.
The ''BigRat.new'' constructor accepts a BigInt numerator and denominator (or just a numerator on its own). However, you can pass in Nums or Strings if a BigInt can be built from them
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.
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.
;General observations
▲The original Javascript
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 or BigRats 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===
|