Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert float64 array to float32 array

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

like image 619
LaSul Avatar asked Nov 05 '25 01:11

LaSul


2 Answers

For example,

package main

func main() {
    var features64 [120]float64

    var features32 [len(features64)]float32
    for i, f64 := range features64 {
        features32[i] = float32(f64)
    }
}
like image 131
peterSO Avatar answered Nov 07 '25 16:11

peterSO


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)
}
like image 37
Adrian Avatar answered Nov 07 '25 16:11

Adrian