Skip to main content

Mojo trait

Floatable

The Floatable trait describes a type that can be converted to a Float.

Any type that conforms to Floatable works with the built-in float function.

This trait requires the type to implement the __float__() method.

For example:

@value
struct Foo(Floatable):
var i: Float64

fn __float__(self) -> Float64:
return self.i
@value
struct Foo(Floatable):
var i: Float64

fn __float__(self) -> Float64:
return self.i

A Foo can now be converted to a Float64 using float:

var f = float(Foo(5.5))
var f = float(Foo(5.5))

Note: If the __float__() method can raise an error, use the FloatableRaising trait instead.

Implemented traits

AnyType

Methods

__float__

__float__(self: _Self) -> SIMD[float64, 1]

Get the float point representation of the value.

Returns:

The float point representation of the value.