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

Missing primitives for signed calculations, and some operations for unsigned calculations #1105

Closed
hashedone opened this issue Sep 24, 2021 · 6 comments

Comments

@hashedone
Copy link
Contributor

hashedone commented Sep 24, 2021

There are no primitives for signed calculations in cosmwasm std, and there are not even "wrapping" operations to simulate those behavior. Not a problem for Uint128, as it can be converted to native u128, and then to i128, but it doesn't help with (de)serialization.

Also Uint256 (and probably Uint128) is missing modulo operation which is useful to have.

@webmaster128
Copy link
Member

What's the problem/use case exactly? Are you suggesting a signed Int128 type that does string serialization in JSON and otherwise is a i128?

There are a bunch of wrapping operations in Uint128 already:

    pub fn wrapping_add(self, other: Self) -> Self {
        Self(self.0.wrapping_add(other.0))
    }

    pub fn wrapping_sub(self, other: Self) -> Self {
        Self(self.0.wrapping_sub(other.0))
    }

    pub fn wrapping_mul(self, other: Self) -> Self {
        Self(self.0.wrapping_mul(other.0))
    }

    pub fn wrapping_pow(self, other: u32) -> Self {
        Self(self.0.wrapping_pow(other))
    }

I'm sure we are missing a ton of things, but what do you need most urgently?

@uint
Copy link
Contributor

uint commented Sep 28, 2021

Note: we should probably do #1114 before tackling this, unless there's something urgent that needs added.

@archseer
Copy link

archseer commented Nov 6, 2021

For Uint128 operations I've been simply unwrapping the value, then re-wrapping it before serializing.

@webmaster128
Copy link
Member

This is now tracked in #1186. cosmwasm-std 1.1 ships many of those additional implementations. Please comment there is something is still missing.

@larry0x
Copy link
Contributor

larry0x commented May 15, 2023

If I'm not mistaken, there still isn't a primitive for signed integer. We currently use this type implemented by Margined protocol. @webmaster128 something worthwhile to be introduced into cosmwasm-std?

@webmaster128
Copy link
Member

Right. The big question is if such a signed integer should be a tuple (unsigned value, sign) or a "proper" signed integer that encodes the sign in the two's complement. I am slightly leaning to the later as I think an Int128 should be consistent with i128 in terms of range and behaviour.

Could you create a dedicated ticket for that topic?

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

5 participants