-
Notifications
You must be signed in to change notification settings - Fork 69
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
Comments
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) |
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.) |
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. |
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. |
Assess for P22. |
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
The text was updated successfully, but these errors were encountered: