format printed SIMD3's

I print a lot of SIMD3 vectors to the debugger area in Xcode. Some vector-elements have prefixed signs and some values are longer than others. I would like the printed vectors to align as they did in my old days of Fortran.

The SIMD3 members are scalars which can't be cast as Double ( I know how to format output for Double) or even as NSNumber.

Something akin to a NumberFormatter for vectors would be the objective, but I don't see how to do it. Scalars are wierd and (helpful) documentation sparse.

  • Do you print in debugger or in Console ?

  • as I understand it, the console of the debugger area (not the variable view, which is to the left of the console). I had to look that up. ;-)

Add a Comment

Replies

I am working a new tac. I use my existing Double extension I call EE() on each of the elements of a simd_double3, which obviously has three double elements. I've abandoned SIMD3 with its scalars.

so:

extension simd_double3 {
    func EE_simd_double3(_ sigDig: Int?,_ toSize: Int?,_ padChar: String?) -> String {
        return "(\((self.x).EE(sigDig,toSize,padChar)), \((self.y).EE(sigDig,toSize,padChar)), \((self.z).EE(sigDig,toSize,padChar)))"
    }
}
extension Double {
    func EE(_ sigDig: Int?,_ toSize: Int?,_ padChar: String?) -> String {
        let formatter = NumberFormatter()
        formatter.numberStyle              = .scientific
        formatter.positivePrefix           = "+"
        formatter.maximumIntegerDigits     = 1
        formatter.usesSignificantDigits    = true
        formatter.minimumSignificantDigits = sigDig  ?? 4
        formatter.maximumSignificantDigits = sigDig  ?? 5
        return formatter.string(from: self as NSNumber)!.pad(padWithString: padChar ?? " ", toSize: toSize ?? 16, prefix: false)    //.padding(toLength: padTo ?? 26, withPad: padChar ?? " ", startingAt: stringNum.count)
    }
}

My remaining problems are how to include the plus sign after the E in scientific nation, e.g., -4.516611095E1 -> -4.516611095E+1 and to specify the number of digits after E, e.g., -4.516611095E1 -> -4.516611095E+01

extension String {
    func pad(padWithString: String, toSize: Int, prefix: Bool) -> String {
        var padChar = " "
        if toSize >= self.count {
            for _ in 0..<(toSize - self.count) {
                padChar = padWithString + padChar
            }
        }
        if prefix { return padChar + self }
        else      { return self + padChar }
    }