# Calculate share of Nats

Let’s say I have two `Nat`s: `A` and `B`
By definition `A` is always bigger than `B`

How can I calculate `X: float32` which is `B / A`?

Motoko doesn’t have `float32`, only `Float`, which is 64-bit.

``````import Float "mo:base/Float";
Float.fromInt(B) / Float.fromInt(A)
``````

`fromInt` works for `Nat`, because `Nat` is a subtype of `Int`.

2 Likes

Yea… sorry for misleading. I’m looking for a rust solution.

Will something like this work?

``````use num_traits::ToPrimitive;
...
result.0.to_f32();
``````

UPD: no it won’t

I’ve figured out your example won’t work also if `A` and `B` are both bigger than 64 bits.

Yes, but since you are losing precision anyway, you can truncate `Nat` to 64 bits before converting to `Float`.

For Rust, you may need infinite precision fraction as well, for example fraction - Rust

You can also decode a Candid `Nat` to `u128` in Rust (decoding will fail if the value is more than 128bit). Might be easier to convert u128 to f32.

1 Like