[swift-evolution] Expanded support for numeric types in stdlib?
Dmitri Gribenko
gribozavr at gmail.com
Thu Dec 3 23:48:32 CST 2015
On Thu, Dec 3, 2015 at 9:22 PM, Austin Zheng <austinzheng at gmail.com> wrote:
> Integer is intended to be used by application developers, and most of the
> arithmetic/comparison functionality exposed by the stdlib deals with
> arguments and return values of type Integer.
So one would use Integer, which is capable of representing arbitrary
precision integers, most of the time? We have considered this in the
past. The concern is that the branches that this introduces
everywhere and the code bloat wouldn't allow Swift to achieve C-like
performance.
We are considering adding an arbitrary precision integer type, though.
> A useful compiler support feature might be a "BigIntegerLiteralConvertible"
> protocol, which allows literal integers out of the range of Ints to be
> assigned to Integer values. Swift can already tell if an integer literal is
> out of range (e.g. "let x : Int8 = 123456"), so there is some precedent.
Swift's integer literal convertible protocol supports integers up to
2048 bits. This is sufficient for all practical purposes, I think.
> Rational numbers are represented by the Rational struct which encapsulates
> two Integers, the numerator and denominator. Rationals can be conditionally
> constructed explicitly from two Integers (as long as the denominator isn't
> 0), or from a single Integer (all Integers are Rationals, but the opposite
> is not true).
Yep, this is pretty standard. How do you propose to model "Integers
are Rationals" in Swift?
> A good invariant for Rationals might be having them always
> represented in the most simplified form - for example, Rational(2, 6) should
> be represented internally as 1/3 when constructed.
This has performance tradeoffs. What do other libraries and languages do?
> Future topics to explore include complex numbers, arbitrary-precision
> floating-point numbers, and fixed-precision and/or decimal number types.
What about protocols, operations and algorithms?
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-evolution
mailing list