4

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

3 Answers 3

3

For example,

package main

func main() {
    var features64 [120]float64

    var features32 [len(features64)]float32
    for i, f64 := range features64 {
        features32[i] = float32(f64)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

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)
}

Comments

2

Simply

var arr1 [120]float64
var arr2 [120]float32
for i, v := range arr1 {
    arr2[i] = float32(v)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.