Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpers for fixed-point / rational / other floating-point replacement #395

Open
jonjove opened this issue Aug 2, 2022 · 5 comments
Open
Assignees

Comments

@jonjove
Copy link
Contributor

jonjove commented Aug 2, 2022

What problem does your feature solve?

We don't have support for floating-point types, but we also don't have any helpers to make it easier to do operations on floating-point-like types.

What would you like to see?

Some helpers, but not sure which ones.

What alternatives are there?

TODO

@heytdep
Copy link
Contributor

heytdep commented Aug 2, 2022

Would it be possible to add helpers to perform basic operations (add and multiply) on fixed-point numbers, could something like the following implementation be included in the SDK?

pub struct FixedPoint(i64);

fn add(left: &FixedPoint, right: &FixedPoint) -> FixedPoint {
    FixedPoint(left.0 + right.0)
}

fn multiply(left: &FixedPoint, right: &FixedPoint) -> FixedPoint {
    let overflow_mask = (1 << 32) - 1;
    let a = left.0 & overflow_mask;
    let b = left.0 >> 32;
    let c = right.0 & overflow_mask;
    let d = right.0 >> 32;
    FixedPoint(((b * d) << 32) + b * c + a * d + (a * c).wrapping_shr(32))
}


fn test() {
    let a = FixedPoint(0x900000000); // 9.
    let b = FixedPoint(0x80000000); // .5
    assert_eq!(FixedPoint(0x480000000).0, multiply(&a, &b).0); // 4.5
}

The above code is a slightly changed version from one I saw in a reddit thread (mainly that FixedPoint operations shouldn't implement the default operators (+, *) so that they don't get confused with other numerical types)

@leighmcculloch
Copy link
Member

Yup I think so. I'm also curious whether there are crates in the general Rust ecosystem that could serve this purpose. Such as: https://docs.rs/fixed. (I haven't used it, I don't know if it is suitable to use yet.)

@mootz12
Copy link
Contributor

mootz12 commented Aug 10, 2022

It looks like https://docs.rs/fixed is a binary fixed point number system. I'm not convinced the performance improvements to math operations are worth the weird rounding behavior of binary fixed point for decimal numbers, instead of just adding decimal fixed point support.

@jayz22
Copy link
Contributor

jayz22 commented Jun 28, 2023

I think we've came to the decision to not support fixed-point numbers as first class host object/function types during the value overhaul (stellar/rs-soroban-env#679). Instead providing fixed-size big integers [iu]128, [IU]256 that can be used to facilitate implementing fixed-point numbers and arithmetic on the guest side. @graydon can confirm or correct me.

Although this issue is about sdk helpers, which we may consider adding. Moving this to Soroban V2.

@anupsdf
Copy link
Contributor

anupsdf commented Apr 8, 2024

Assess for P22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants
@graydon @leighmcculloch @jayz22 @jonjove @mootz12 @heytdep @anupsdf and others