Converting a negative value to a positive

Everything else that doesn't fall into one of the other PB categories.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Converting a negative value to a positive

Post by Hroudtwolf »

Hallo Comunity,

Can anyone explain me , how to convert a negative value to a positive ?


Thanks for your answer ...
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Code: Select all

value = -value
El_Choni
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

That works in pb?

In general you will need to do something more like:

value=value*-1;

Which is what Choni's example does anyway, but this is non-specific to PB.
User avatar
Le Soldat Inconnu
Enthusiast
Enthusiast
Posts: 306
Joined: Wed Jul 09, 2003 11:33 am
Location: France

Post by Le Soldat Inconnu »

Or use the ABS() function, see the help
LSI
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

thanks @ALL.

I got the answer already in PureBasic-Lounge.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

At least in x86 CPUs multiplying a value takes more time than negating it. In my mind, too :mrgreen:
El_Choni
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

dracflamloc wrote:That works in pb?

In general you will need to do something more like:

value=value*-1;

Which is what Choni's example does anyway, but this is non-specific to PB.
What do you mean?

Code: Select all

#include <stdio.h>

int main(int argc, char* argv[])
{
	long value = 12345;

	value *= -1;
	printf("value: %d\n", value);

	value =- value;
	printf("value: %d\n", value);

	value =~ value+1;
	printf("value: %d\n", value);

	return 0;
}
Good programmers don't comment their code. It was hard to write, should be hard to read.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

"value = -value" is probably the fastest (and easiest) way to reverse sign in PB.
User avatar
Falko
Enthusiast
Enthusiast
Posts: 271
Joined: Sat Oct 04, 2003 12:57 pm
Location: Germany
Contact:

Post by Falko »

:wink:
Last edited by Falko on Fri Apr 08, 2005 8:24 am, edited 1 time in total.
www.falko-pure.de
Win11 Pro 64-Bit, PB_6.11b1
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

i'm too lazy to answer


sorry only in german
SPAMINATOR NR.1
leo1991
New User
New User
Posts: 7
Joined: Tue Feb 24, 2004 7:25 pm

Post by leo1991 »

Rings wrote:i'm too lazy to answer


sorry only in german
:o :o :?:
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

:lol: nice link Rings ...
Post Reply