QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Bert22306 on March 01, 2021, 05:35:18 am

Title: I feel like I'm going out of my mind
Post by: Bert22306 on March 01, 2021, 05:35:18 am
Surely, this trivial program should be giving the same result, for x, y, and  z??

Code: [Select]
Screen _NewImage(120, 43, 0)
For i = 1 To 10
    x = 15 * i / 3000
    y = i / 3000 / 15
    z = i / (3000 / 15)
    Print x, y, z
Next i
End

Was going bonkers trying to debug something, and the problem came down to the above, when distilled to its basics. What am I forgetting? Thanks!
Title: Re: I feel like I'm going out of my mind
Post by: SMcNeill on March 01, 2021, 06:22:34 am
Forgetting basic math?

1 / 2 / 3 isn't the same as 3 * 1 / 2....

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(120, 43, 0)
  2. FOR i = 1 TO 10
  3.     x = 15 * i / 3000
  4.     y = i / 3000 * 15
  5.     z = i / (3000 / 15)
  6.     PRINT x, y, z
Title: Re: I feel like I'm going out of my mind
Post by: Bert22306 on March 01, 2021, 07:21:28 am
Forgetting something, for sure. Best to be generous with parentheses. Thanks, Steve!

It was a case of, 1 / 2 / 3 is not the same as 1 / (2 / 3)

In fact, 1 / (2 / 3) is the same as 3 * 1 / 2.

That was my issue.