Would you ever need to create an instance of the Constants
class? No. So it shouldn't be a class.
A more fitting way to define your constants would be as static properties of Double
, just like pi
is defined as a static property of Double
.
extension Double {
// The Dirac constant in Joule-seconds, equal to the Planck constant divided by 2π.
static let diracConstant = 1.054_571_8e-34 // in Joule-seconds
// Or you can spell it this way if you want.
static let hBar = 1.054_571_8-34
// Or even this way! ħ is Unicode U+0127 LATIN SMALL LETTER H WITH STROKE
static let ħ = 1.054_571_8e-34
}
Then you can access it explicitly like this:
-2 * m / (Double.diracConstant * Double.diracConstant)
-2 * m / (Double.hBar * Double.hBar)
-2 * m / (Double.ħ * Double.ħ)
Presumably, m
is already of type Double, so you can use “dot syntax” to make the expression even shorter:
-2 * m / (.diracConstant *.diracConstant)
-2 * m / (.hBar *.hBar)
-2 * m / (.ħ *.ħ)
Or if you define a square
function:
func square(_ x: Double) -> Double { return x * x }
Then you can say this:
-2 * m / square(.diracConstant)
-2 * m / square(.hBar)
-2 * m / square(.ħ)
If you're using Float
instead of Double
for your calculations, you can do all of this with the Float
type instead.