num: add %+v support to Format method, extend tests and fix found bug
MODIFY
num/dual/dual.go
num/dual/dual.go
@@ -33,6 +33,10 @@ func (d Number) Format(fs fmt.State, c rune) {
fmt.Fprintf(fs, "%T{Real:%#v, Emag:%#v}", d, d.Real, d.Emag)
return
}
+ if fs.Flag('+') {
+ fmt.Fprintf(fs, "{Real:%+v, Emag:%+v}", d.Real, d.Emag)
+ return
+ }
c = 'g'
prec = -1
fallthrough

MODIFY
num/dualquat/dual.go
num/dualquat/dual.go
@@ -40,6 +40,10 @@ func (d Number) Format(fs fmt.State, c rune) {
fmt.Fprintf(fs, "%T{Real:%#v, Dual:%#v}", d, d.Real, d.Dual)
return
}
+ if fs.Flag('+') {
+ fmt.Fprintf(fs, "{Real:%+v, Dual:%+v}", d.Real, d.Dual)
+ return
+ }
c = 'g'
prec = -1
fallthrough

MODIFY
num/hyperdual/hyperdual.go
num/hyperdual/hyperdual.go
@@ -33,7 +33,11 @@ func (d Number) Format(fs fmt.State, c rune) {
switch c {
case 'v':
if fs.Flag('#') {
- fmt.Fprintf(fs, "%T{Real:%#v, E1mag:%#v, E2mag:%#v, E1E2mag%#v}", d, d.Real, d.E1mag, d.E2mag, d.E1E2mag)
+ fmt.Fprintf(fs, "%T{Real:%#v, E1mag:%#v, E2mag:%#v, E1E2mag:%#v}", d, d.Real, d.E1mag, d.E2mag, d.E1E2mag)
+ return
+ }
+ if fs.Flag('+') {
+ fmt.Fprintf(fs, "{Real:%+v, E1mag:%+v, E2mag:%+v, E1E2mag:%+v}", d.Real, d.E1mag, d.E2mag, d.E1E2mag)
return
}
c = 'g'

MODIFY
num/quat/quat.go
num/quat/quat.go
@@ -34,6 +34,10 @@ func (q Number) Format(fs fmt.State, c rune) {
fmt.Fprintf(fs, "%T{Real:%#v, Imag:%#v, Jmag:%#v, Kmag:%#v}", q, q.Real, q.Imag, q.Jmag, q.Kmag)
return
}
+ if fs.Flag('+') {
+ fmt.Fprintf(fs, "{Real:%+v, Imag:%+v, Jmag:%+v, Kmag:%+v}", q.Real, q.Imag, q.Jmag, q.Kmag)
+ return
+ }
c = 'g'
prec = -1
fallthrough

MODIFY
num/quat/quat_test.go
num/quat/quat_test.go
@@ -96,6 +96,8 @@ var formatTests = []struct {
}{
{q: Number{1.1, 2.1, 3.1, 4.1}, format: "%#v", want: "quat.Number{Real:1.1, Imag:2.1, Jmag:3.1, Kmag:4.1}"}, // Bootstrap test.
{q: Number{-1.1, -2.1, -3.1, -4.1}, format: "%#v", want: "quat.Number{Real:-1.1, Imag:-2.1, Jmag:-3.1, Kmag:-4.1}"}, // Bootstrap test.
+ {q: Number{1.1, 2.1, 3.1, 4.1}, format: "%+v", want: "{Real:1.1, Imag:2.1, Jmag:3.1, Kmag:4.1}"},
+ {q: Number{-1.1, -2.1, -3.1, -4.1}, format: "%+v", want: "{Real:-1.1, Imag:-2.1, Jmag:-3.1, Kmag:-4.1}"},
{q: Number{1, 2, 3, 4}, format: "%v", want: "(1+2i+3j+4k)"},
{q: Number{-1, -2, -3, -4}, format: "%v", want: "(-1-2i-3j-4k)"},
{q: Number{1, 2, 3, 4}, format: "%g", want: "(1+2i+3j+4k)"},

num: add %+v support to Format method, extend tests and fix found bug