Why this isn't working


(Chad Caglak) #1

hi all, just wondering why type casting isn’t working

fails on 3, 4, NaN

type Kind int

const (
NaT Kind = iota + 1 // not a triangle
Equ                 // all sides equal
Iso                 // two sides equal
Sca                 // all sides different
)

func KindFromSides(a, b, c float64) Kind {

ma := map[int]interface{}{
	1: a,
	2: b,
	3: c,
}

for i := 1; i <= 3; i++ {

	if _, ok := ma[i].(string); ok {
		return NaT
	}

}

if a <= 0 || b <= 0 || c <= 0 {
	return NaT
}

if (a+b+c)/3 == a {
	if (a+b+c)/3 == b {
		if (a+b+c)/3 == c {
			return Equ
		}
	}
}

if a+b < c || b+c < a || c+a < b {
	return NaT
}

if a == b || b == c || c == a {
	return Iso
}

return Sca
}

(Norbert Melzer) #2

This code seems to compile, could you please tell us what you mean by “it isn’t working”?


(Chad Caglak) #3

hi
trying to return NaT when user inputs (3, 4, NaN) invalid input


(Norbert Melzer) #4

You need to use math.IsNaN() to check if one your values is not a number.


(Chad Caglak) #5

thanks a lot

for i := 1; i <= 3; i++ {

	if b1 := math.IsNaN(ma[i].(float64)); b1 {
		return NaT
	}
	if b2 := math.IsInf(ma[i].(float64), 0); b2 {
		return NaT
	}

}