NEWTON

NEWTON


Popular tags

    Cairo: How would I store negative numbers then retrieve them as negatives?

    Asked

    4 months ago

    8

    views


    1

    I am trying to store a negative number, and then on a function call, read it. But I only get the positive felt number.

    This question was originally posted on Triality

      cairocairo-lang

    3 answers

    0

    Since all felts operate around modulo P, a negative number is naturally equal to some positive number modulo P. So if you are planning on just doing some addition or general math in your contract with that number, you should be fine. If you are bringing that number to the front end I can imagine that would be tricky. Then you might want to use a struct as mentioned in other answers or to save on storage costs, if you can afford not using the entire felt you can designate the first bit to be an indicator of positive or negative.

    

    

    This answer was originally posted on Triality

    answered

    4 months ago

    1

    Cairo:我将如何存储负数然后将它们检索为负数?

    struct MyStruct:

    

    member is_negative : felt

    

    member value : felt

    

    end

    This answer was originally posted on Triality

    answered

    4 months ago

    0

    You can use a struct where one member is the sign, and one is the value. ex:

    struct MyStruct:

    member is_negative : felt

    member value : felt

    end

    This answer was originally posted on Triality

    answered

    4 months ago

    Your answer

    NEWTON

    NEWTON