I have an array of float64 and want to convert each value to float32.
I've tried:
# What I have
features64 [120]float64
# What I've tried
features32 = [120]float32(features64)
But that gives the compile error:
cannot convert features (type [120]float64) to type [120]float32
For example,
package main
func main() {
var features64 [120]float64
var features32 [len(features64)]float32
for i, f64 := range features64 {
features32[i] = float32(f64)
}
}
You can't convert one slice/array type to another. You'll need to create a new array and iterate over the original converting each element:
for i,f := range features64 {
features32[i] = float32(f)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With