# [swift-evolution] Epic: Typesafe calculations

Matt Whiteside mwhiteside.dev at gmail.com
Wed Jan 13 22:27:05 CST 2016

```> On Jan 13, 2016, at 11:16, Stephen Canon <scanon at apple.com> wrote:
>
> … in exact arithmetic, not in floating-point, which is what people tend to actually use for matrices.  For that matter, the entire notion of “invertible” as a binary attribute is problematic when dealing with approximate arithmetic; a matrix may be formally invertible but so ill-conditioned that the result is effectively meaningless.
>
> Not really trying to shoot you down, just observing that this is an extremely subtle example with lots of hidden sharp edges.
>
> – Steve

That’s a good point.  So then I wonder if exact arithmetic wouldn’t be good enough for a lot of entry level math & science applications, especially the ones where this type of approach would come to mind in the first place.

Something like,

struct Rational:CustomDebugStringConvertible{
let numerator, denominator: Int
var debugDescription:String{
get{
return "\(Double(numerator)/Double(denominator))"
}
}
}

func *(lhs:Rational,rhs:Rational) -> Rational{
return Rational(numerator: lhs.numerator*rhs.numerator,
denominator: lhs.denominator * rhs.denominator)
}

func *(lhs:Int,rhs:Rational) -> Rational{
return Rational(numerator: lhs*rhs.numerator,
denominator: rhs.denominator)
}

let π = Rational(numerator: 355, denominator: 113)
let two_π = 2 * π

might get you pretty far with 64 bits.

Matt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160113/7700bdfe/attachment.html>
```