NEWTON

NEWTON


Popular tags

    Does Cairo support int256, or negative numbers in general?

    Asked

    4 months ago

    13

    views


    0

    If not, are there any workarounds?

    This question was originally posted on Triality

      cairo-langcairo

    2 answers

    0

    You can also see on the cairo docs that a felt is an integer in the range -P/2 to P/2, where P is a very large (prime) number (currently it is a 252-bit number, which is a number with 76 decimal digits)

    https://www.cairo-lang.org/docs/hello_cairo/intro.html#field-element

    This answer was originally posted on Triality

    answered

    5 months ago

    0

    If you plug the following into a cairo script or the cairo playground: https://www.cairo-lang.org/playground/

    You will get the following output:

    Program output:

    -1

    -2

    3

    -7

    

    Number of steps: 44

    %builtins output range_check

    from starkware.cairo.common.serialize import serialize_word

    from starkware.cairo.common.math_cmp import is_le

    

    

    func main{output_ptr : felt*, range_check_ptr}():

    let x = -1

    let y = x + x

    let z = y * y + x

    let (a) = negative_nums(x)

    serialize_word(x)

    serialize_word(y)

    serialize_word(z)

    serialize_word(a)

    return ()

    end

    

    

    #Show that negative numbers work fine with calculations

    func negative_nums{range_check_ptr}(inp : felt) -> (res : felt):

    let (isLe) = is_le(inp, 0)

    if isLe == 1:

    return (res=-5 + -2)

    end

    return (res=-4)

    end

    

    Hopefully this shows that you can work with negative numbers just fine! Add any clarifications if there are any issues

    This answer was originally posted on Triality

    answered

    5 months ago

    Your answer

    NEWTON

    NEWTON