Question about Arithmetic Operation (changing the sign)

Post Reply
JonCodesBad
Trader
Posts: 50
Joined: Mon Apr 13, 2015 7:58 pm
Location: SW England.

Question about Arithmetic Operation (changing the sign)

Post by JonCodesBad »

I have a piece of code that works, but I'm not convinced I'm going about it correctly.
In the code below "Max_Lot_Diff" is a positive extern.. eg 0.25. I need to test -0.25 in this snippet. Is there a better/correct way?

Code: Select all

if(s==0 && Net_Position > -Max_Lot_Diff)
Cheers

Jon
User avatar
milanese
TechAdmin
Posts: 3293
Joined: Wed Jan 09, 2013 9:02 am
Location: btr rdx, r8 +

Question about Arithmetic Operation (changing the sign)

Post by milanese »

To switch between positive and negative values, just multiply the value by (-1).

Cheers :)

Tommaso
Global Prime is the official SHF broker :yahoo:
Searching for Servers and Workstations with individual configuration?
Just PM
:smile: Click here to go to the BoardKnowledgeBase
NOTE: Cookies and JavaScript are required for the using the board, with full functionality
JonCodesBad
Trader
Posts: 50
Joined: Mon Apr 13, 2015 7:58 pm
Location: SW England.

Question about Arithmetic Operation (changing the sign)

Post by JonCodesBad »

Thanks Tommaso. :hi: :good:
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Question about Arithmetic Operation (changing the sign)

Post by SteveHopwood »

I occasionally have to present users with an input that needs to be a negative number and check that they have have remembered the "-" sign. For example:

extern double MaxLossAllowed = -100;

Then I add a check in OnInit():

Code: Select all

if (MaxLossAllowed > 0)
          MaxLossAllowed*= -1;
:xm:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
JonCodesBad
Trader
Posts: 50
Joined: Mon Apr 13, 2015 7:58 pm
Location: SW England.

Question about Arithmetic Operation (changing the sign)

Post by JonCodesBad »

Thanks Steve. :hi: :good:
User avatar
pascalx
Trader
Posts: 20
Joined: Mon Mar 28, 2016 2:40 pm

Question about Arithmetic Operation (changing the sign)

Post by pascalx »

For simplicity, I would just write

a = 5;
a = -a;

However, Steve's approach can be better, because on some compilers you might save 1 instruction. However, we talk about 2-3 instructions here, so it should be of no concern in real use case.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Question about Arithmetic Operation (changing the sign)

Post by SteveHopwood »

pascalx » Thu Mar 31, 2016 12:08 pm wrote:For simplicity, I would just write

a = 5;
a = -a;

However, Steve's approach can be better, because on some compilers you might save 1 instruction. However, we talk about 2-3 instructions here, so it should be of no concern in real use case.
Just for your interest, there was a time when either mine or your method did not work. I am not sure which one it was, but I think it was mine and that yours was the only one to adopt back then. I have this vague folk-memory of trying my method again recently because it makes sense to me, and being pleased to find it works.

The Cretins at Crapperquotes do occasionally sort something really stupid out. Not often mind. :arrrg:

:xm:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
Post Reply

Return to “Coders Hangout”