[swift-evolution] Implicit Type Conversion For Numerics Where Possible.

davesweeris at mac.com davesweeris at mac.com
Wed Mar 30 16:48:22 CDT 2016

> On Mar 30, 2016, at 12:57 PM, Ted F.A. van Gaalen via swift-evolution <swift-evolution at swift.org> wrote:
> Thank you, Robert & Haravikk
> Please allow me to respond in-line hereunder, thanks.
> Ted.
>> On 30.03.2016, at 16:15, Haravikk <swift-evolution at haravikk.me <mailto:swift-evolution at haravikk.me>> wrote:
>> I’m in favour of implicit conversion for integers where no data can be lost (UInt32 to Int64, Int32 to Int64 etc.), in fact I posted a similar thread a little while ago but can’t find it; there’s something being done with numbers so this may be partly in the works.
>> I definitely think that implicit conversion for floating point should be avoided, as it can’t be guaranteed
> Why?  and   What cannot be guaranteed? 

These all fail with a runtime error ("Playground execution aborted: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”, if you’re curious):

And this loop runs for 1024 iterations before finding just one instance where the conversion from Int to Double and back to Int actually gives the right answer:
var i = UInt(Int.max) + 1
var remainder = UInt()
repeat {
    i -= 1
    let DoubleI = UInt(Double(i))
    remainder = DoubleI > i ? DoubleI - i : i - DoubleI
} while remainder != 0

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160330/f8af9ce9/attachment.html>

More information about the swift-evolution mailing list