16

Is it possible to square root 50 or should I use 7.071067.

I'm very new in programming and I also understand that it's possible in all other languages, but I'm not able to see where the limitations are.

17

An implementation of a square root function was added to the dapp-bin as part of a maths library a while back.

Have a look at PR#50 (which is actually still open). I think the discussions boil down to the following:

function sqrt(uint x) returns (uint y) {
    uint z = (x + 1) / 2;
    y = x;
    while (z < y) {
        y = z;
        z = (x / z + z) / 2;
    }
}

Which is the Babylonian Method of finding the square root.

| improve this answer | |
  • 2
    You're amazing, I will use Babylonian methods in the future. – Sileniced Apr 12 '16 at 19:32
  • Until the PR is merged, see here: github.com/ethereum/dapp-bin/pull/50/files for the latest implementation. – Justin Harris Jul 25 '18 at 20:07
  • Note error is big for small numbers, as Solidity doesn't deal with floats. One way to reduce the error, if your calculations only rely on mul/div, is to multiply the input by 10000, and once your done mul/div the result, divide it by 100. – Fabiano Soriani Jan 21 '19 at 0:13
5

Currently we are talking about implementing fixed point exponentiation which means that yes, in the near future, it should be possible to implement square roots and roots of all kinds...with one caveat...they cannot be infinitely repeating...you will have to cast them to a fixed size so you might lose some accuracy on it.

There is not, and will likely never be a floating point value running on ethereum....it's simply too expensive to make that happen. So we have chosen the fixed point route. Should be up within the coming weeks.

Without that...yes, there is something in the math library, but I'm afraid that without a fixed point value, there's not alot to be done with it.

| improve this answer | |
  • 1
    fixed point square root can be very useful. It's not a requirement to have floating point, you can roll your own fixed point implementation (I just multiply everyhing by 1e6 myself). Be happy when I don't have to do that however. – Paul S Apr 12 '16 at 23:18
  • it's Jan 2019, unfortunately the near future never came – Fabiano Soriani Jan 20 '19 at 23:49
4

There are no floats in solidity as far as I know. There's probably some code somewhere for calculating square roots using integer representations of floats, but that would be outside solidity. I'm about 80% certain of this answer, so someone who knows more might want to respond as well.

| improve this answer | |
0

I've started working on a fixed-point math library for solidity. It's open source (apache 2) and you're invited to use and contribute. This library can solve your problem by providing a set of basic math functions such as log, power, root, etc., that use fixed point decimal numbers of the kind customarily used for ERC20 tokens and automatic market makers.

Please find the code here: https://github.com/extraterrestrial-tech/fixidity

It's low on documentation right now, so please contact me if you have any questions.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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