ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Expression Difference? (https://www.excelbanter.com/excel-programming/366147-expression-difference.html)

FCC[_14_]

Expression Difference?
 

Hello,

I was wondering how come these two expression have different results


Code:
--------------------

12 - (previousSickMonth - employmentMonth + tempMonth)

--------------------



Code:
--------------------

12 - previousSickMonth - employmentMonth + tempMonth

--------------------


The first one gives me the correct answer that I want. But it doesn't
make sense to me how come there would be a difference. All the
operators are just subtraction and addition, and it shouldn't be based
on any form of precedence.

Is there some kind of VBA difference from other programming languages I
am missing here?


--
FCC
------------------------------------------------------------------------
FCC's Profile: http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981


Tim Williams

Expression Difference?
 
What answers do you get, and from what inputs?
They should give the same result, unless perhaps your inputs are of different types and precisions.

Tim.


"FCC" wrote in message ...

Hello,

I was wondering how come these two expression have different results


Code:
--------------------

12 - (previousSickMonth - employmentMonth + tempMonth)

--------------------



Code:
--------------------

12 - previousSickMonth - employmentMonth + tempMonth

--------------------


The first one gives me the correct answer that I want. But it doesn't
make sense to me how come there would be a difference. All the
operators are just subtraction and addition, and it shouldn't be based
on any form of precedence.

Is there some kind of VBA difference from other programming languages I
am missing here?


--
FCC
------------------------------------------------------------------------
FCC's Profile: http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981




NickHK

Expression Difference?
 
The expressions are not the same, hence the different results. If you
multiply out the brackets in the first example, what do you get ?
It's not VBA, it's your maths.

NickHK

"FCC" wrote in message
...

Hello,

I was wondering how come these two expression have different results


Code:
--------------------

12 - (previousSickMonth - employmentMonth + tempMonth)

--------------------



Code:
--------------------

12 - previousSickMonth - employmentMonth + tempMonth

--------------------


The first one gives me the correct answer that I want. But it doesn't
make sense to me how come there would be a difference. All the
operators are just subtraction and addition, and it shouldn't be based
on any form of precedence.

Is there some kind of VBA difference from other programming languages I
am missing here?


--
FCC
------------------------------------------------------------------------
FCC's Profile:

http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981




NickHK

Expression Difference?
 
You get a difference of 2 * ABS(employmentMonth - tempMonth) between the 2
expressions ?

NickHK

"FCC" wrote in message
...

Hello,

I was wondering how come these two expression have different results


Code:
--------------------

12 - (previousSickMonth - employmentMonth + tempMonth)

--------------------



Code:
--------------------

12 - previousSickMonth - employmentMonth + tempMonth

--------------------


The first one gives me the correct answer that I want. But it doesn't
make sense to me how come there would be a difference. All the
operators are just subtraction and addition, and it shouldn't be based
on any form of precedence.

Is there some kind of VBA difference from other programming languages I
am missing here?


--
FCC
------------------------------------------------------------------------
FCC's Profile:

http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981




FCC[_15_]

Expression Difference?
 

Try some input like:

previousSickMonth = 1
employmentMonth = 5
tempMonth = 2

With

Code:
--------------------

tempMonth = 12 - previousSickMonth - employmentMonth + tempMonth

--------------------


I got 8.

With

Code:
--------------------

tempMonth = 12 - (previousSickMonth - employmentMonth + tempMonth)

--------------------

I got 14.

But I know what the problem is, if you work out the bracklets first,
you could get an negative number. Obviously a subtract a negative
number and you get a positive number, hence the difference.

But to be honest, this is the first time I've ever experienced a
difference in two expressions that consisted of only addition and
subtraction operators.

I've always been under the impression that bracklets don't mean
anything with only addition and subtraction operators. I guess I was
wrong.


--
FCC
------------------------------------------------------------------------
FCC's Profile: http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981


NickHK

Expression Difference?
 
If you think of it as:
tempMonth = 12 - 1 * (previousSickMonth - employmentMonth + tempMonth)

does it make it more clear ?

NickHK

"FCC" wrote in message
...

Try some input like:

previousSickMonth = 1
employmentMonth = 5
tempMonth = 2

With

Code:
--------------------

tempMonth = 12 - previousSickMonth - employmentMonth + tempMonth

--------------------


I got 8.

With

Code:
--------------------

tempMonth = 12 - (previousSickMonth - employmentMonth + tempMonth)

--------------------

I got 14.

But I know what the problem is, if you work out the bracklets first,
you could get an negative number. Obviously a subtract a negative
number and you get a positive number, hence the difference.

But to be honest, this is the first time I've ever experienced a
difference in two expressions that consisted of only addition and
subtraction operators.

I've always been under the impression that bracklets don't mean
anything with only addition and subtraction operators. I guess I was
wrong.


--
FCC
------------------------------------------------------------------------
FCC's Profile:

http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981




FCC[_16_]

Expression Difference?
 

A little bit, but in reality everything is mulitplied by 1 so still
doesn't really bring out the essence of the bracklets.


--
FCC
------------------------------------------------------------------------
FCC's Profile: http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981


NickHK

Expression Difference?
 

12 - (previousSickMonth - employmentMonth + tempMonth)
=12 - 1 * (previousSickMonth - employmentMonth + tempMonth)
=12 - previousSickMonth + employmentMonth - tempMonth <<< Note change
of sign
< 12 - previousSickMonth - employmentMonth + tempMonth

NickHK

"FCC" wrote in message
...

A little bit, but in reality everything is mulitplied by 1 so still
doesn't really bring out the essence of the bracklets.


--
FCC
------------------------------------------------------------------------
FCC's Profile:

http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981




FCC[_18_]

Expression Difference?
 

Crap. I am an idiot. Of course. Multiplying the bracklets with the
negative changes the signs.

Duh. Back to grade 1 math for me. :rolleyes:

Thanks for the clarification.


--
FCC
------------------------------------------------------------------------
FCC's Profile: http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981


NickHK

Expression Difference?
 
I have those brain freeze moments....seem to be increasing in frequency...

NickHK

"FCC" wrote in message
...

Crap. I am an idiot. Of course. Multiplying the bracklets with the
negative changes the signs.

Duh. Back to grade 1 math for me. :rolleyes:

Thanks for the clarification.


--
FCC
------------------------------------------------------------------------
FCC's Profile:

http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981




Tim Williams

Expression Difference?
 
Me too...

Tim

"FCC" wrote in message ...

Crap. I am an idiot. Of course. Multiplying the bracklets with the
negative changes the signs.

Duh. Back to grade 1 math for me. :rolleyes:

Thanks for the clarification.


--
FCC
------------------------------------------------------------------------
FCC's Profile: http://www.excelforum.com/member.php...o&userid=35888
View this thread: http://www.excelforum.com/showthread...hreadid=557981





All times are GMT +1. The time now is 03:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com