NEWTON
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
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
Does Cairo support int256, or negative numbers in general?
How to use Kakarot for my Solidity Contracts?
How do you check if a number is even in Cairo?
Cairo Passing Functions
Cairo: What is the difference between let and tempvar?
How can i learn Cairo with Python base?
Cairo: Getting function selector
How do I run a Pathfinder node?
Is Kakarot a Starkware project?
Issue with generating _abi.json file
Cairo Automatic Events
Is there any blockchain project contract supporting web2 scripts executable on VIa EVM?
Did not have to pay gas fee when calling same function multiple times in Starknet?
Invalid response while using `ec.getKeyPair("copy