1

I'm working on a Substrate-based blockchain project and I've run into an issue related to type information and encoding. Specifically, I'm trying to use an f64 field within a struct that also uses Substrate's TypeInfo trait, but I'm getting a compilation error.

Here's a simplified version of my struct:

#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct LabelInfo<AccountId, Hashing, Balance> {
    pub owner: AccountId,
    pub public_key: BoundedVec<u8, ConstU32<128>>,
    pub reputation: f64,
    pub stake: Balance,
    pub staking_address: BoundedVec<u8, ConstU32<128>>,
    pub status: RegisterStatus,
}

The error message I'm getting is:

the trait bound f64: scale_info::TypeInfo is not satisfied

The compiler also suggests that several other types do implement TypeInfo, such as i128, i64, u128, and u64.

How can I resolve this issue? Can f64 be made to satisfy TypeInfo, or is there an alternative approach to achieve a similar outcome?

1 Answer 1

1

We normally never use floats in the runtime because of their possible interdeminism.
This is why TypeInfo is not default implemented for floats.
You can use PerBill or any of the other PerThing implementations to deterministically represent fractions within the range [0, 1].
For normal fractions there is FixedI128 and alike: FixedPointNumber.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.