package reflect import "math" // OverflowComplex reports whether the complex128 x cannot be represented by v's type. // It panics if v's Kind is not [Complex64] or [Complex128]. func (v Value) OverflowComplex(x complex128) bool { k := v.kind() switch k { case Complex64: return overflowFloat32(real(x)) || overflowFloat32(imag(x)) case Complex128: return false } panic(&ValueError{"reflect.Value.OverflowComplex", v.kind()}) } // OverflowFloat reports whether the float64 x cannot be represented by v's type. // It panics if v's Kind is not [Float32] or [Float64]. func (v Value) OverflowFloat(x float64) bool { k := v.kind() switch k { case Float32: return overflowFloat32(x) case Float64: return false } panic(&ValueError{"reflect.Value.OverflowFloat", v.kind()}) } func overflowFloat32(x float64) bool { if x < 0 { x = -x } return math.MaxFloat32 < x && x <= math.MaxFloat64 } // OverflowInt reports whether the int64 x cannot be represented by v's type. // It panics if v's Kind is not [Int], [Int8], [Int16], [Int32], or [Int64]. func (v Value) OverflowInt(x int64) bool { k := v.kind() switch k { case Int, Int8, Int16, Int32, Int64: bitSize := v.typ().Size() * 8 trunc := (x << (64 - bitSize)) >> (64 - bitSize) return x != trunc } panic(&ValueError{"reflect.Value.OverflowInt", v.kind()}) } // OverflowUint reports whether the uint64 x cannot be represented by v's type. // It panics if v's Kind is not [Uint], [Uintptr], [Uint8], [Uint16], [Uint32], or [Uint64]. func (v Value) OverflowUint(x uint64) bool { k := v.kind() switch k { case Uint, Uintptr, Uint8, Uint16, Uint32, Uint64: bitSize := v.typ_.Size() * 8 // ok to use v.typ_ directly as Size doesn't escape trunc := (x << (64 - bitSize)) >> (64 - bitSize) return x != trunc } panic(&ValueError{"reflect.Value.OverflowUint", v.kind()}) }