0

I'm new to Solidity and I'm having some troubles with type issues. I'm trying to execute the following functions:

function sigmoidSingle(int128 x) public pure returns(int128)
{
    //The sigmoid function is f(x) = 1 / (1 + e^(-x))
    x =  ONE + ABDKMath64x64.exp(-x);
    int128 y = ABDKMath64x64.div(ONE,x);
    return y;
    
}

//Sigmoid activation function to process a np array
function sigmoid(int128[] memory x) public pure returns(int128[] memory)
{
    uint xSize = x.length;
    int128[] memory y_Preds = new int128[](xSize);
    
    for(uint8 i = 0; i < xSize; i++ )
    {
         y_Preds[i] =  sigmoidSingle(x[i]);
    }
   
    
    return y_Preds;
}

where the input array x of the sigmoid function is an array of 64.64 fixed point format numbers. I am converting the numbers with Python taking advantage of the following function:

def toFixedPoint64(x):
    x = x * (2**64)
    y = round(x + 0.5)
    return y

Now, assume that i want to convert the following array of floating point numbers:

[0.52,0.49,-0.20,0.57]

The resulting array would be:

[9592306918328967168, 9038904596117680128, -3689348814741910528, 10514644122014443520]

When i give in input this array to sigmoid function, i'm getting the following error:

Error encoding arguments: Error: invalid number value (arg="", coderType="int128", value=9592306918328967000, version=4.0.44)

I don't get why I am having this issue, indeed with an int128 i should be able to represent higher values of this one. Also i don't get why the number is cut to 9592306918328967000. Consider that if I give in input the number 9592306918328967168 to the sigmoidSingle function i don't get any problem, on the contrary it returns the correct result. Now, I suspect that the problem may arise because of the array, but since I'm not a Solidity expert I don't know what to think. Is there anyone who can help me understand the situation?

UPDATE: It is a problem of the VM simulator. Giving the number surrounded by quotation marks should work. Ex: "9592306918328967168" is the correct way to give in input this int128 to Remix GUI.

0

On Python 2, the round() function returns a floating point number (see here).

Switch to Python 3, or use int(round(x + 0.5)) instead.

| improve this answer | |
  • I'm already using python3 and getting properly an integer. The problem is that the sigmoid function doesn't accept the resulting numbers, but i don't understand why, since the numbers should be representable as int128 – jack_5 Apr 12 at 20:29
0

Just put all the number into double quotes:

[ "9592306918328967168",
  "9038904596117680128",
  "-3689348814741910528",
  "10514644122014443520" ]

as your numbers are too big to be represented as JSON numbers.

With these quoted values, the output of sigmoid function is:

11568834341578123390, // 0.627147766313195582813
11438944655894070866, // 0.620106432343090129282
8304097042261566175,  // 0.450166002687522088675
11783100815680795823  // 0.638763175148841909118

Also, there is a security issue with your code as -x is unsafe and may cause overflow. You better use ABDKMath64x64.neg(x) instead.

BTW, you may use ABDK Toolkit to convert numbers to/from 64.64 format.

P.S. I'm the author of this ABDKMath64x64 library.

| 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.