Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default 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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default 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



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default 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



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default 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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
i have expression - E$5 what is the difference from E5? Dmitrii Excel Discussion (Misc queries) 4 May 13th 23 07:46 PM
value expression RayB Excel Worksheet Functions 7 January 17th 07 09:57 PM
need help with expression Peterpunkin Excel Discussion (Misc queries) 5 May 4th 06 05:56 PM
Expression gudway New Users to Excel 3 May 4th 06 01:12 PM
charting a difference of 2 columns' w/o adding a difference column Wab Charts and Charting in Excel 4 July 27th 05 02:37 AM


All times are GMT +1. The time now is 03:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"