# what wrong with this code?

7312

```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!```
6803

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'.

Moderator
10070

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;```

simplified

I can't see any problem with casting as Point is a double

168

`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 * PipValue

What is being added and what multiplicated ?

value + ( ordergap * Point ) * PipValue + ( newsgap * Point ) * PipValue

might 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.

15085

aakcaagac:

value + ( ordergap * Point ) * PipValue + ( newsgap * Point ) * PipValue

might 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)

168

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.
15085

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)
168

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 calculated

30 + 3 = 33

Summed up these values and later on multiplied it with a different value.

33 * 3 = 99

Then 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 = 39

Now 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

15085

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```