English

## what wrong with this code?

 273 sheriffonline 2015.11.23 13:48   ```double value=0; int ordegap=3; int newsgap=3 value=value+ordergap*Point*PipValue+newsgap*Point*PipValue; is the above code i use correctly? because i dont get correct value!```
 2017 gooly 2015.11.23 13:55 #   1) ordergap*Point are points but what is PipValue - probably they don't match.2) ordergap*Point*PipValue are e.g. \$ - and value is it \$ as well?otherwise you 'compare apple with oranges'.
 Moderator3418 GumRai 2015.11.23 14:06 #   We don't know the value for PipValue.We don't know what logic you are using or what you expect to achieve.We don't know what value you get and what value you are expecting.You need to give more details.Your code is the same as```  double value=0;   int ordegap=3;   int newsgap=3   value=(ordergap+newsgap)*Point*PipValue;``` simplifiedI can't see any problem with casting as Point is a double
 145 aakcaagac 2015.11.23 19:50 #   `value=value+ordergap*Point*PipValue+newsgap*Point*PipValue;` This looks like a simple solution. The first thing you should do is, check whether the Math rules are set properly. Point calculation usually comes before adding and subtracting calculation. This is the first thing that I would look up. I would also put some parenthesis "(" and ")" around the Math, to make sure that your expected calculations are correct.E.g.What do you calc here:value + ordergap * Point * PipValue + newsgap * Point * PipValueWhat is being added and what multiplicated ?value + ( ordergap * Point ) * PipValue + ( newsgap * Point ) * PipValuemight return a different value than:value + ordergap * ( Point * PipValue )  + newsgap * ( Point * PipValue )Make sure you read yourself about operator priorities. That should solve the problem.
 12998 WHRoeder 2015.11.24 13:55 #   aakcaagac: value + ( ordergap * Point ) * PipValue + ( newsgap * Point ) * PipValuemight return a different value than:value + ordergap * ( Point * PipValue )  + newsgap * ( Point * PipValue )False. Multiplication has higher priority then addition A * B * C + D * E * F = (A * B) * C + (D * E) * F = A * (B * C) + D *( E * F) = (A * B * C) + (D * E * F)
 145 aakcaagac 2015.11.24 14:26 #   WHRoeder: False. Multiplication has higher priority then addition A * B * C + D * E * F = (A * B) * C + (D * E) * F = A * (B * C) + D *( E * F) = (A * B * C) + (D * E * F)Not false! I exactly wanted to point out that Multiplication has a highter priority than addition Otherwise I wouldn't have referenced the link to the "operand priorities". The point is: What does the initial poster want to achieve? If he pretends that his calculations return wrong values then it's most likely that "he" calculated it wrong. What he calculates on paper by his own does not necessarily met the priority or math requirements of operand priorities.
 12998 WHRoeder 2015.11.24 18:59 #    Wrong. "might return a different value than" is false. it make no difference in the order of multiplies. (1 * 2 ) * 3 = 2 * 3 = 6 = 1 * 6 = 1 * (2 * 3)
 145 aakcaagac 2015.11.24 19:10 #   WHRoeder:  Wrong. "might return a different value than" is false. it make no difference in the order of multiplies. (1 * 2 ) * 3 = 2 * 3 = 6 = 1 * 6 = 1 * (2 * 3)You are right with what you explain. But that's not what I wanted to express in my initial reply. Though I might have chosen a better example.As I tried to explain in my second reply, it depends on how the initial poster has made his calculus. It could be that the initial poster wasn't aware of the calculation rules or oversight that when doing his own calculus on dry run (e.g. on a piece of paper) and then afterwards tried bringing this in a formula.As example:He might have calculated30 + 3 = 33Summed up these values and later on multiplied it with a different value.33 * 3 = 99Then he might have taken these values into a formula and wondered why the result differs to those he initially calculated on paper:30 + 3 * 3 = 39Now he is sitting at home and can't figure out why the value 39 is not the expected sum of 99 that he initially tried to calculate... To whatever reasons...Therefore I came up with the reference to operand priorities and the hint by using parenthesis in his calculations to give the stuff some priorities).That might look as in our example like this:(30 + 3) * 3 = 99
12998
WHRoeder 2015.11.24 19:42 #

aakcaagac: You are right with what you explain. But that's not what I wanted to express in my initial reply.
1. I didn't disagree with the rest so didn't comment on it.
2.  Parentheses are important. ```4 > 3 > 2 true > 2    1  > 2    false```