| View previous topic :: View next topic |
| Author |
Message |
*Worthy* Critical Thinker

Joined: 11 Aug 2003 Posts: 186
|
Posted: Mon Aug 23, 2004 7:45 pm Post subject: |
|
|
6 * 3 / 2 = 9
3 / 2 * 6 = 9
3 * 6 / 2 = 9
Normally, you are right. But here's what will happen in the plotscripting compiler:
6 * 3 / 2 =
18 / 2 = 9
3 / 2 * 6 =
1 * 6 = 6
3 * 6 / 2 =
18 / 2 = 9
In math used outside of OHR, however, you are right. But the early rounding for plotscripting alters the end result. (Or so I think)
~Worthy _________________ You can do whatever you want...but prison is full of people who make bad decisions. |
|
| Back to top |
|
 |
Moogle1 Scourge of the Seas Halloween 2006 Creativity Winner


Joined: 15 Jul 2004 Posts: 3377 Location: Seattle, WA
|
Posted: Mon Aug 23, 2004 7:48 pm Post subject: |
|
|
It does. A script in SG3 depends on this error. _________________
|
|
| Back to top |
|
 |
TMC On the Verge of Insanity
Joined: 05 Apr 2003 Posts: 3240 Location: Matakana
|
Posted: Mon Aug 23, 2004 10:14 pm Post subject: |
|
|
| Quote: | | Order of ops doesn't matter when you're only dealing with multiplication and division. |
Other way around. You really only need to worry about this when you are dealing with operations of the same importance of order, such as multiplication and division, because in these cases the order in which the operations are done depends in which order you write them.
Quoting from plotscr.hsd:
| Code: | define operator,begin
20 ^ exponent
30,mod,modulus
30 / divide
30 * multiply
40 -- subtract
40 + add
50 == equal
50 <> notequal
50 << lessthan
50 >> greaterthan
50 <= lessthanorequalto
50 >= greaterthanorequalto
60,xor,xor
60,and,and
60,or,or
70,:=,setvariable
end
|
Operations with the smaller number are calculated first, two operations with the same value are calculated in whichever order you write them, where the danger is.
Of interest is 'and' and 'or', which as James pointed out recently, can be used incorrectly in conditional statements. _________________ "It is so great it is insanely great." |
|
| Back to top |
|
 |
Flamer The last guy on earth...

Joined: 04 Feb 2003 Posts: 725 Location: New Zealand (newly discovered)
|
Posted: Tue Aug 24, 2004 12:41 am Post subject: |
|
|
I don't think it's an error. I mean, Java does the same thing if you're working with integer variables(unless you convert it to double, string or other)
it only accepts integers and truncades the decimals out _________________ If we were a pack of dogs, IM would be a grand Hound, CN would be a very ficious little pitball, and Giz...well, it doesn't matter breed he is, he'd still be a bitch
(no offense to anyone that was mentioned) |
|
| Back to top |
|
 |
TMC On the Verge of Insanity
Joined: 05 Apr 2003 Posts: 3240 Location: Matakana
|
Posted: Tue Aug 24, 2004 12:58 am Post subject: |
|
|
Actually, that's true. _________________ "It is so great it is insanely great." |
|
| Back to top |
|
 |
Inferior Minion Metric Ruler

Joined: 03 Jan 2003 Posts: 741 Location: Santa Barbara, CA
|
Posted: Tue Aug 24, 2004 2:41 am Post subject: |
|
|
But it does matter depending on how the compiler handles integer division.
3 / 2 = 1.5
If the compiler truncates the integer mid calculation then 3 / 2 = 1.
Therefore 3 / 2 * 6 = 6 and 6 * 3 / 2 = 9 _________________
|
|
| Back to top |
|
 |
Moogle1 Scourge of the Seas Halloween 2006 Creativity Winner


Joined: 15 Jul 2004 Posts: 3377 Location: Seattle, WA
|
Posted: Tue Aug 24, 2004 4:37 am Post subject: |
|
|
This is useful in some cases. Say you need to know if a number is divisible by another:
(x / 2 * 2 == x)
is true if x is divisible by 2, false otherwise. _________________
|
|
| Back to top |
|
 |
|