# Use custom float-like class with numba

**URL:** <https://numba.discourse.group/t/use-custom-float-like-class-with-numba/2735>\
**Category:** Support: How do I do ...?\
**Created:** [September 18, 2024, 3:02pm UTC](https://numba.discourse.group/t/use-custom-float-like-class-with-numba/2735 "2024-09-18T15:02:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eendebakpt](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/eendebakpt/32/1132_2.png) [@eendebakpt](https://numba.discourse.group/u/eendebakpt)\
**Post date:** [September 18, 2024, 3:02pm UTC](https://numba.discourse.group/t/use-custom-float-like-class-with-numba/2735/1 "2024-09-18T15:02:42Z")

</div>

How can I let numba treat a custom class as a float? A minimal examle

```auto
import numba
import numpy as np

class MyFloat:    
    def __init__ (self, f):
        self.f = f
        
    def __float__ (self) -> float:       
        return float(f)
    
    # ... in the actual implementation more is defined to make the class work like a float

s = 1.1
x = np.array([1,2,3.])
m = MyFloat(1.12)

@numba.jit(nopython=True)
def f(x, a):
    return a + x**2

f(s, 2) # works!
f(x, 2) # works!
f(m, 2) # fails with TypingError: non-precise type pyobject

```

I can use `forceobj=True` to make the call `f(m, 2)` work, but that makes the calls to `f(s, 2)` and `f(x, 2)` very slow. Is there a way to specify that numba should treat objects of type `MyFloat` like a Python float (with conversion by calling ` __float__ `)?

I have read the `Interval` example at [Example: an interval type — Numba 0.52.0.dev0+274.g626b40e-py3.7-linux-x86\_64.egg documentation](https://numba.pydata.org/numba-doc/dev/extending/interval-example.html). But there it looks like one would have to teach numba about all the operations one could do with a `MyFloat`, instead of converting `m` to a float and then using the normal jitted method.

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [September 25, 2024, 3:13am UTC](https://numba.discourse.group/t/use-custom-float-like-class-with-numba/2735/2 "2024-09-25T03:13:04Z")

</div>

Just to clarify… the Interval example gives you the information you need?

---

<div class="post-metadata">

**Author:** ![eendebakpt](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/eendebakpt/32/1132_2.png) [@eendebakpt](https://numba.discourse.group/u/eendebakpt)\
**Post date:** [September 25, 2024, 8:31am UTC](https://numba.discourse.group/t/use-custom-float-like-class-with-numba/2735/3 "2024-09-25T08:31:03Z")

</div>

@nelson2005 The Interval example does not provide the information I am looking for. I am looking for a way for Numba to treat my `MyFloat` class as a builtin float. So in this example:

```auto
@numba.jit(nopython=True)
def poly(x, a, b):
     return a * x + b

poly(1.1, 2.1, 3)
poly(1.1, MyFloat(2.1), 3)

```

The first call `poly(1.1, 2.1, 3)` is jitted by numba efficiencty. For the second call `poly(1.1, MyFloat(2.1), 3)` I would like numba to:

> i) Convert MyFloat(2.1) to a python float (or a numba float64) using `MyFloat. __float__ `  
> ii) Then call `poly(1.1, 2.1, 3)` the way numba normally handles two float arguments

Here both i) and ii) are fast operations, so I would hope numba can jit it much faster then the regular python call.

Note: writing `poly(1.1, float(MyFloat(2.1)), 3)` does not work, since I do not know beforehand the type of the argument `a`. It could also be a numpy array, in which case casting to `float` is not the right approach.
