Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: [SOLVED] Return the negative value of an integer  (Read 2124 times)

torbente

• Sr. Member
• Posts: 307
[SOLVED] Return the negative value of an integer
« on: July 16, 2018, 07:41:46 pm »
Im using this function, but i want know if this can be done diferently:

Code: Pascal  [Select][+][-]
1. function GetNegativeValue(number:Integer):Integer;
2. Begin
3. if number > 0 then Result := number-(Number*2)
4. else Result := number;
5. End;
« Last Edit: July 16, 2018, 08:23:04 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

GetMem

• Hero Member
• Posts: 3986
Re: Return the negative value of an integer
« Reply #1 on: July 16, 2018, 07:45:16 pm »
Code: Pascal  [Select][+][-]
1.  Number :=  Abs(Number)*(-1);

Gammatester

• Jr. Member
• Posts: 69
Re: Return the negative value of an integer
« Reply #2 on: July 16, 2018, 07:52:00 pm »
Or simply
Code: Pascal  [Select][+][-]
1. GetNegativeValue := -abs(number)
Note that your original (number*2) might overflow.

torbente

• Sr. Member
• Posts: 307
Re: Return the negative value of an integer
« Reply #3 on: July 16, 2018, 08:22:43 pm »
Code: Pascal  [Select][+][-]
1.  Number :=  Abs(Number)*(-1);
Or simply
Code: Pascal  [Select][+][-]
1. GetNegativeValue := -abs(number)
Note that your original (number*2) might overflow.

Both solutions better than mine. Thanks a lot.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet