Sebastian answers your question (+1), but there might be other simple functional solutions that leverage existing generic functions (such as reduce, which is frequently used for sum-like calculations). Perhaps:
let π = [Int](0 ..< 625)
.map { Double($0 * 4 + 1) }
.reduce(0.0) { return $0 + (8.0 / ($1 * ($1 + 2.0))) }
Or:
let π = [Int](0 ..< 625).reduce(0.0) {
let x = Double($1 * 4 + 1)
return $0 + (8.0 / (x * (x + 2.0)))
}
These both generate an answer of 3.14079265371779, same as your routine.
If you really want to write your own generic function that does this (i.e. you don't want to use an array, such as above), you can simplify the process by getting the + operator out of the sum function. As Sebastian's answer points out, when you try to perform addition on a generic type, you have to jump through all sorts of hoops to define a protocol that specifies that the types that support + operator and then define these numeric types as conforming to that Addable protocol.
While that's technically correct, if you have to define each numeric type that you might use in conjunction with this sum function as being Addable, I'd contend that this is no longer in the spirit of generic programming. I'd suggest you rip a page out of the reduce book, and let the closure that you pass to this function do the addition itself. That eliminates the need to need to define your generics as being Addable. So that might look like (where T is the type for the sum that will be calculated and U is the type for index that is being incremented):
func apply<T, U: Comparable>(initial: T, term: (T, U) -> T, a: U, next: (U) -> U, b: U) -> T {
let value = term(initial, a)
if a < b {
return apply(value, term, next(a), next, b)
} else {
return value
}
}
And you'd call that like so:
let π = apply(Double(0.0), { return $0 + 8.0 / Double((Double($1) * Double($1 + 2))) }, 1, { $0 + 4}, 2500)
Note, it's no longer a sum function as it's really just a "repeat executing this closure from a to b", hence my choice to rename this to apply.
But as you can see, we've moved the addition into the closure we pass to this function. But it gets you out of the silliness of having to redefine every numeric type you want to pass to the "generic" function as being Addable.
Note, this also addresses another problem that Sebastian solved for you, namely the need to implement neutralAdditionElement for every numeric data type, too. That was necessary to perform a literal translation of the code you provided (i.e. to return zero if a > b). But I've changed the loop such that rather than returning zero when a > b, it returns the calculated term value if a == b (or greater).
Now, this new generic function can be used with any numeric types, without needing to implement any special functions or make them conform to any special protocols. Frankly, there's still room for improvement here (I'd probably considering using a Range or SequenceType or something like that), but it gets to the your original question of how to use generic functions to calculate the sum of a series of evaluated expressions. To make it behave in a truly generic manner, the answer is probably "get the + out of the generic function and move it to the closure".