Math Algorithm Bug
Author | Topic: Math Algorithm Bug |
---|---|
Apprentice
Member # 4258
|
written Tuesday, May 4 2004 21:39
Profile
I just found a strange little bug in the math algorithm... I tested these 2 lines of code: print_num(24 - 21 + 1); print_num(1 + 24 - 21); They *should* both produce a result of 4, yet the first line subtracts the 1 instead of adding it and produces a result of 2. The second line works fine however. Posts: 28 | Registered: Wednesday, April 14 2004 07:00 |
Post Navel Trauma ^_^
Member # 67
|
written Tuesday, May 4 2004 21:58
Profile
Homepage
I bet it's doing 24 - (21 + 1) and 1 + (24 - 21). Still a bug, but that'll be why. When precedence matters, you should probably use brackets just to be on the safe side. -------------------- Barcoorah: I even did it to a big dorset ram. New Mac BoE Posts: 1798 | Registered: Thursday, October 4 2001 07:00 |
Board Administrator
Member # 1
|
written Tuesday, May 4 2004 22:48
Profile
Homepage
It's because subtraction mistakenly has equal precedence with addition, and operations are done from right to left. The first thing will be fixed, but operations will continue to be right to left. - Jeff -------------------- Official Board Admin spidweb@spiderwebsoftware.com Posts: 960 | Registered: Tuesday, September 18 2001 07:00 |