# Problem with "while"

#1

Hi guys,
I wanted to solve this equation, and for that, I used while. What did I do wrong?

<?php
echo "1.5 + 0.02x <= 1.1 + 0.03x" . "
";
\$a = 1.5;
\$b = 1.1;
\$x = 0;
while (\$a > \$b) {
\$a = \$a + 0.02;
\$b = \$b + 0.03;
\$x++;
}
print "x" . " >= " . \$x; //the \$x should be 40, not 41. What did I do wrong?
?>

#2

Same thing in JavaScript.

``````console.log("1.5 + 0.02x <= 1.1 + 0.03x");
\$a = 1.5;
\$b = 1.1;
\$x = -1;
while (\$b < \$a) {
\$x++;
\$a += 0.02;
\$b += 0.03;
}
console.log(\$x + " <= x");``````

console:

`````` >
1.5 + 0.02x <= 1.1 + 0.03x
40 <= x``````

#3

It boils down to one of the `x-ticks` being discounted since it is an edge case that must overflow. I'm still wrapping my head around this. The algebra is pretty straight forward but the algo is introducing an extra tick.

``0.4 <= 0.01 * x``

#4

Hi mtf,
I put my \$x = -1 as you did, and it worked as I wanted. But I don't understand why it must be \$x = -1 and I'll tell you why.
For example, every single time the 1.1 gains 0.03, the 1.5 also gains 0.02 and the \$x works as a counter and it is added 1.
So if \$x = 1 , \$a = 1.52 and \$b = 1.13.
So when \$x = 40, \$a = 1.5 + 0.02 * 40 = 2.3 , \$b = 1.1 + 0.03 * 40 = 2.3 ⇒ \$a = \$b
if \$a = \$b, \$a is no longer > \$b and the while should NOT be ran again. and the \$x should be = 40.
Anyway, if I put \$x = -1, I am saying that when \$a = 1.52 and \$b = 1,13, the x = 0, aren't I? So I thought that the final \$x would be 39, but it is 40

#5

One needs to run through this with a pencil and paper, or simulate the loop and print the values that are generated in a table. What it looks like is that the last increment increases the count by one more than it is supposed to be. To discover where this happens, we need that table.

As we can see, I fudged the program to make it work, but the logic of that fudging is still nebulous. Later on I'll see if I can draw up the table so we can examine it. Or you can. Post the code back here, if you do, please.

#6

Hi mtf,
I did what you asked. Run the program and see it. It doesn't stop when the \$x = 40, it goes one more even with the \$a being equal to \$b , and our while condition was \$a > \$b and that shouldn't have happened. Strange...
OBSERVATION: I didn't manage to put the paragraph icon "br" inside the "<>", so before control c control v the program to test it , put the "< >" icons in the appropriate places. It's just 2 "br" that needs this fixing.
.

``````<?php
echo "1.5 + 0.02x <= 1.1 + 0.03x";
echo "<br>";
\$a = 1.5;
\$b = 1.1;
\$x = 0;
while (\$a > \$b) {
\$a = \$a + 0.02;
\$b = \$b + 0.03;
\$x++;
print \$a . " --- " . \$b . " --- " . \$x . "<br>";
}
?>``````

#7

Tried a slightly different print statement:

``````<?php
echo "1.5 + 0.02x <= 1.1 + 0.03x";
echo "<br>";
\$a = 1.5;
\$b = 1.1;
\$x = 0;
while (\$b < \$a) {
print \$a . " --- " . \$b . " --- " . \$x++ . "<br>";
\$a += 0.02;
\$b += 0.03;
}
print \$x;
?>``````

Output

``````1.5 + 0.02x <= 1.1 + 0.03x>
1.5 --- 1.1 --- 0
1.52 --- 1.13 --- 1
1.54 --- 1.16 --- 2
1.56 --- 1.19 --- 3
1.58 --- 1.22 --- 4
1.6 --- 1.25 --- 5
1.62 --- 1.28 --- 6
1.64 --- 1.31 --- 7
1.66 --- 1.34 --- 8
1.68 --- 1.37 --- 9
1.7 --- 1.4 --- 10
1.72 --- 1.43 --- 11
1.74 --- 1.46 --- 12
1.76 --- 1.49 --- 13
1.78 --- 1.52 --- 14
1.8 --- 1.55 --- 15
1.82 --- 1.58 --- 16
1.84 --- 1.61 --- 17
1.86 --- 1.64 --- 18
1.88 --- 1.67 --- 19
1.9 --- 1.7 --- 20
1.92 --- 1.73 --- 21
1.94 --- 1.76 --- 22
1.96 --- 1.79 --- 23
1.98 --- 1.82 --- 24
2 --- 1.85 --- 25
2.02 --- 1.88 --- 26
2.04 --- 1.91 --- 27
2.06 --- 1.94 --- 28
2.08 --- 1.97 --- 29
2.1 --- 2 --- 30
2.12 --- 2.03 --- 31
2.14 --- 2.06 --- 32
2.16 --- 2.09 --- 33
2.18 --- 2.12 --- 34
2.2 --- 2.15 --- 35
2.22 --- 2.18 --- 36
2.24 --- 2.21 --- 37
2.26 --- 2.24 --- 38
2.28 --- 2.27 --- 39
2.3 --- 2.3 --- 40
41``````

#8

Insteresting. That showed that the while is working perfectly and that in the end, the \$x gained 1 and was print alone. So I just need to do this (below) so the 1 that the \$x gained in the final, he loses it back:

``````<?php
echo "1.5 + 0.02x <= 1.1 + 0.03x";
echo "<br>";
\$a = 1.5;
\$b = 1.1;
\$x = 0;
while (\$a > \$b) {
\$a = \$a + 0.02;
\$b = \$b + 0.03;
\$x++;
}
print "x" . " >= " . (\$x - 1);``````

thanks for the help
and sorry for my stupidity

#9

Northing of the sort, I see quite the opposite, here.