# is this the right way to simulate having a negative number?

I needed a number which could go negative but since there is no negative number in solidity this is how I'm doing it:

``````// using safe Math

uint pool = 0; // this is my number
bool positivePool = true; // to know if the pool number is negative or positive

function updatePool(uint _amount, bool _positive) public {
if (positivePool) {
if (_positive {
}
else {
if (_amount > pool) {
pool = _amount.sub(pool);
positivePool = false;
} else {
pool = pool.sub(_amount);
}
}
} else {
if (_positive) {
if (_amount > pool) {
pool = _amount.sub(pool);
positivePool = true;
} else {
pool = pool.sub(_amount);
}
}
else {
}
}
}
``````

now this is the simplest way that i could think of. my question is if this is the right way or not? it feels like there should be an easier way.

is there anything wrong with this code?

• Don't rewrite the question after it has been answered, because it renders the accepted answer obsolete! The fact that you didn't know about negative numbers in Solidity doesn't imply anything negative about you. Nov 4, 2019 at 13:14
• @goodvibration i changed my question because all the codes in it were just useless, my old question was implying what is the best way to use negative numbers with a wrong example of my wrong way to do it, my new edited question is directly asking for the best way of having negative numbers, not only it's shortened but also easier for other users to understand. and the answer is still completely valid. so what use other user's would have with my useless and uncorrect codes from my old question? none, would just confuse them.(and well yes also it wouldn't make me look newbie, everyone is happy) Nov 4, 2019 at 13:34
• You are aware of the fact that the old contents is visible (forever), by clicking the `edited` link above... right? Nov 4, 2019 at 14:17
• @goodvibration I am, why do you think i'm so ashamed of my question? main reason i edited it was because it was useless content, and i replaced it with the main/real question. Nov 4, 2019 at 14:46
• Well, I think that the question in its present form is useless and meaningless. And on top of that, if anything, it reflects you in a much more negative light than before. First of all, what does it even mean "best solution for negative numbers"? Second, it makes one think that you haven't bothered to open the docs and learn even the basics of Solidity (and as you know, users here tend to be quite unforgiving for apparent lack of effort). Now, I know you've made an effort, you've written code, and posted it in details, explaining your issue pretty well. There's no shame in that. Nov 4, 2019 at 14:58

Call me an idiot, but I believe you can use negative numbers in Solidity.

Data Type Keyword: `int`

Here is an example I just wrote for you:

``````pragma solidity ^0.5.12;

contract TestContract {
int public globalNumber;

// Add a positive or negative number to globalNumber
function addToGlobalVar(int num) public returns (int){
globalNumber += num;
return globalNumber;
}
}
``````

As you see you can pass a negative number in the function and the declared `int` variable gets negative.

As with most programming languages there are `int` and `uint`.

The `u` means "unsigned", thus it can't store negative values. This doesn't apply to `int` so this will work out for you.

Hope this helps.

• I just checked older Solidity versions and it works for them as well.
– cqx
Nov 4, 2019 at 12:30