Curious Artila Gorvin Posted May 5, 2004 Share Posted May 5, 2004 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. Link to comment Share on other sites More sharing options...
Well-Actually War Trall Khoth Posted May 5, 2004 Share Posted May 5, 2004 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. Link to comment Share on other sites More sharing options...
Well-Actually War Trall Spidweb Posted May 5, 2004 Share Posted May 5, 2004 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 Link to comment Share on other sites More sharing options...
Recommended Posts