I wrote a few number extensions for unit conversions, for example:
public extension Double {
public var dbamp: Double {
return pow(10, self/20)
}
}
public extension Int {
public var dbamp: Double {
return Double(self).dbamp
}
}
For computing a linear gain multiplier from a db value. The problem is, intuitively I'd expect (and want to be able to write) something like -6.dbamp
to return the result for a value of (-6).dbamp
, whereas instead the result is -(6.dbamp)
.
The swift documentation states that
Negative integers literals are expressed by prepending a minus sign (-) to an integer literal, as in -42.
However, it seems that -42
isn't really an integer literal, but an expression involving the -
operator and the integer literal 42
. Perhaps this behaviour is not unique to Swift.
I suppose it may be possible to get the desired behaviour using custom precedence / associativity somehow, but I feel rather nervous doing anything that may be liable to cause potentially obscure bugs down the line for the sake of a dash of syntactic sugar. If it would be necessary to alter precedence of -
operator, that seems clearly not a price worth paying...