How to convert numpy array items to mojo Float?

Hi, i’m trying to get each element of a numpy array as a mojo Float.

So far this works
var value: Float64 = Xb.item(i, j).to_float64()

But the linter sugest using float(obj) wich i assume is Float64()

When i do the conversion like this
var value: Float64 = Float64(Xb.item(i, j))

I get a conversion error of PythonObject to Float64. Any idea why? Can it be done without using mojo’s tensors?

What version are you using?

There is a float() function, similar to the int() function. These were (very recently) deprecated in favor of using the constructors (Int(), Float64(), etc.)

In 24.6, SIMD didn’t have a constructor that took a Floatable value, so you’d have to use float() or to_float64(). In the latest nightly, you should be able to do Float64(item) … If you’re on nightly and that’s not working for you, please open a bug.

2 Likes

Hi arthur.

I’m not on the nightly branch, mojo version: mojo 24.6.0.

Guess i’ll try the nigthly branch or wait till the next release.

Ah, yeah. In that case, you could use float(Xb.item(i, j) to quiet the linter until you update.

Will do, thanks Arthur

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.