<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 13, 2016, at 3:51 AM, Dale Buckley via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">We all know the problems with floating point types, they are well documented and well understood by developers everywhere. We know when to use them and (hopefully) when not to use them and if you are working with financial values on a Foundation based platform you know to not use a floating point type and to use NSDecimalNumber instead.<br class=""><br class="">My question is this; should there be an equivalent NSDecimalNumber type built into Swift that can be used for precision sensitive decimal values where floating point types can’t be used?<br class=""><br class="">Maybe the answer isn’t an ‘equivalent’ as such, it would probably have a modern twist so I can see it looking like something else entirely, but the point still stands.<br class=""><br class="">I feel like there is a need for this, it’s not a new problem and has been solved many times over in other languages such as Objective-C (NSDecimalNumber) and Java (BigDecimal) etc. Yet as it stands we seem to be lacking an equivalent solution in Swift. It shouldn’t be left for an external library to solve, it’s one of those things that need to be built into the language.<br class=""><br class="">What are peoples thoughts on this?<br class=""></div></div></blockquote><br class=""></div><div>Decimal libraries would be great. However, there are a few different kinds of decimal that are interesting—currency tends to have different domain-specific behavior from general floating-point decimal, for instance. One thing I'd like to see us eventually fix at the language level is the low-level protocol for decimal literals. The current behavior of `FloatLiteralConvertible` is to pass the floating-point value down as a binary floating-point Double, making it unsuitable for use by decimal libraries. A more accurate protocol might break a decimal literal down into mantissa/exponent, something like this:</div><div><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>protocol DecimalLiteralConvertible {</div><div>&nbsp; associatedtype Value: IntegerLiteralConvertible</div><div>&nbsp; associatedtype Exponent: IntegerLiteralConvertible</div><div>&nbsp; init(decimalLiteral: Value, timesTenToThePowerOf: Exponent)</div><div>}</div></blockquote><div class=""><br class=""></div><div class="">Then e.g. '0.1' would compile down to 'T(decimalLiteral: 1, timesTenToThePowerOf: -1)' rather than 'T(floatLiteral: 0x1.999999999999ap-4)'. This would make it possible for libraries to extend the language with good decimal support.</div><br class=""><div class="">-Joe</div></body></html>