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

solidity: std math library #50

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

VoR0220
Copy link
Member

@VoR0220 VoR0220 commented Nov 21, 2015

I've included a lot of basic functions...binomial is one that I'm currently trying to figure out but not being able to utilize a 2D array makes this more difficult.

@obscuren obscuren changed the title Even better math library changes that all compile :D solidity: std math library Nov 22, 2015
@chriseth
Copy link
Contributor

Can you combine than with the other math library? And perhaps we could use this sqrt function:

contract c {
    uint bla;
    /// @why3 ensures { to_int result * to_int result <= to_int arg_x < (to_int result + 1) * (to_int result + 1) }
    function sqrt(uint x) returns (uint y) {
        if (x == 0) return 0;
        else if (x <= 3) return 1;
        uint z = (x + 1) / 2;
        y = x;
        while (z < y)
        /// @why3 invariant { to_int !_z = div ((div (to_int arg_x) (to_int !_y)) + (to_int !_y)) 2 }
        /// @why3 invariant { to_int arg_x < (to_int !_y + 1) * (to_int !_y + 1) }
        /// @why3 invariant { to_int arg_x < (to_int !_z + 1) * (to_int !_z + 1) }
        /// @why3 variant { to_int !_y }
        {
            y = z;
            z = (x / z + z) / 2;
        }
    }
}

@VoR0220
Copy link
Member Author

VoR0220 commented Nov 23, 2015

I can absolutely change the sqrt function, but what do you mean by combine with the other math library?

@@ -8,4 +9,167 @@ library Math {
x = mulmod(x, x, m);
}
}

/// @dev unsigned constant infinity (largest number possible)
function Infinity() constant returns (uint inf) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not really behave as "infinity". What about uint_max? (also applies to the others)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After taking some time to understand how Infinity works in other languages, I think I'd agree here. Perhaps this fits better trying to make a global object in Solidity itself? Just shooting out ideas. Either way, point taken and I will update accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, we might allow access to constant public variables at some point.

@alexvandesande
Copy link

I would like to include an example on the homestead site that uses a square root and to do it properly I'd love to include it as an example of importing a library.

Can we merge and deploy this to the live net?

@alexvandesande
Copy link

Also I'm curious about how this can calculate a square root, it's fascinating..

while (z < y) {
y = z;
z = (x / z + z) / 2;
}

edit: Found it. This is awesome!

@michael-spengler
Copy link

    function sqrt(uint256 x) public pure returns (uint256 y) {
        uint256 z = (x + 1) / 2;
        y = x;
        while (z < y) {
            y = z;
            z = (x / z + z) / 2;
        }
        return y;
    }

https://ethereum.stackexchange.com/a/2913/65406

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

Successfully merging this pull request may close these issues.

4 participants