math question

Questions about MultiCharts and user contributed studies.
User avatar
arnie
Posts: 1594
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 514 times

math question

Postby arnie » 19 Jun 2010

I was never a math enthusiastic so I apologise for this question :roll: :

Can we multiply by 0.333333 instead of divide by 3.
I want to avoid the division by zero error, and since I'm multiplying by a fixed value, I was hoping that this could resolve the problem.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Postby TJ » 19 Jun 2010

cautious programmers always use multiplication when possible...

I use *0.5 instead of /2.

*0.33 is just as good as /3 for most situations.



ps.
a side benefit: multiplication is a faster operation than division.
if there is a computer scientist out there, please confirm if this is true.

User avatar
arnie
Posts: 1594
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 514 times

Postby arnie » 19 Jun 2010

thanks TJ

I did a couple of tests and the results were more accurate when multiplied by 0.333333 than 0.33 :roll:

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Postby TJ » 19 Jun 2010

thanks TJ

I did a couple of tests and the results were more accurate when multiplied by 0.333333 than 0.33 :roll:
that would be more accurate for sure,
but if I have to deal with precision at that level,
my trading would suffer.

;-)

User avatar
arnie
Posts: 1594
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 514 times

Postby arnie » 19 Jun 2010

but if I have to deal with precision at that level...

me and my perfectionism... :oops:

for me, things must be equal to the cent, otherwise, is not the same thing

:P
:oops:

SP
Posts: 465
Joined: 06 Feb 2006
Has thanked: 36 times
Been thanked: 286 times

Postby SP » 20 Jun 2010

Use * Reciprocal(3) instead

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: math question

Postby janus » 22 Jun 2010

I was never a math enthusiastic so I apologise for this question :roll: :

Can we multiply by 0.333333 instead of divide by 3.
I want to avoid the division by zero error, and since I'm multiplying by a fixed value, I was hoping that this could resolve the problem.
How does this avoid the division by zero error? If you are using a variables as follows:

a = x/b;

and you wanted to avoid a division of zero with b = 0 then using multiplication will not make any difference. Reason is to convert the above to a multiplication you would have:

a = x * reciprocal(b);

Guess what? When b = 0 then you still get a division by 0 error. The only way to avoid the error is to have a test as follows:

if b <> 0 then begin
  • a = x/b;
end else begin
  • do something else and/or print a warning message and/or alert and/or halt
    "something else" could be setting "a" to a very large value, such as:
    a = 100000000;
    but that depends on the subsequent use of the variable "a".
end;


Return to “MultiCharts”