stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Question about Arithmetic Operation (changing the sign)
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4665
Page 1 of 1
Author:  JonCodesBad [ Fri Mar 25, 2016 10:57 pm ]
Post subject:  Question about Arithmetic Operation (changing the sign)

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
Author:  milanese [ Sat Mar 26, 2016 7:25 am ]
Post subject:  Question about Arithmetic Operation (changing the sign)

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

Cheers :)

Tommaso
Author:  JonCodesBad [ Sat Mar 26, 2016 8:34 am ]
Post subject:  Question about Arithmetic Operation (changing the sign)

Thanks Tommaso. :hi: :good:
Author:  SteveHopwood [ Sat Mar 26, 2016 9:54 am ]
Post subject:  Question about Arithmetic Operation (changing the sign)

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:
Author:  JonCodesBad [ Sat Mar 26, 2016 3:26 pm ]
Post subject:  Question about Arithmetic Operation (changing the sign)

Thanks Steve. :hi: :good:
Author:  pascalx [ Thu Mar 31, 2016 12:08 pm ]
Post subject:  Question about Arithmetic Operation (changing the sign)

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.
Author:  SteveHopwood [ Fri Apr 01, 2016 6:35 pm ]
Post subject:  Question about Arithmetic Operation (changing the sign)

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:
All times are UTC Page 1 of 1