ToFloat32Array converts the value to float32 slice if it's a Float32Array.
()
| 981 | |
| 982 | // ToFloat32Array converts the value to float32 slice if it's a Float32Array. |
| 983 | func (v *Value) ToFloat32Array() ([]float32, error) { |
| 984 | if !v.IsFloat32Array() { |
| 985 | return nil, errors.New("value is not a Float32Array") |
| 986 | } |
| 987 | |
| 988 | buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo() |
| 989 | defer buffer.Free() |
| 990 | |
| 991 | totalSize := uint(byteOffset + byteLength) |
| 992 | bytes, err := buffer.ToByteArray(totalSize) |
| 993 | if err != nil { |
| 994 | return nil, err |
| 995 | } |
| 996 | |
| 997 | data := bytes[byteOffset : byteOffset+byteLength] |
| 998 | result := make([]float32, len(data)/4) |
| 999 | for i := 0; i < len(result); i++ { |
| 1000 | bits := binary.LittleEndian.Uint32(data[i*4:]) |
| 1001 | result[i] = math.Float32frombits(bits) |
| 1002 | } |
| 1003 | return result, nil |
| 1004 | } |
| 1005 | |
| 1006 | // ToFloat64Array converts the value to float64 slice if it's a Float64Array. |
| 1007 | func (v *Value) ToFloat64Array() ([]float64, error) { |