By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Can PHP run decimal loop step, such as 0.1, correctly?

P: n/a
I want to use the following code to print all value of alpha + beta _
gamme = 1;

But I miss some of the results. I suspect PHP cannot run decimal loop
correctly. I should get 66 groups.

$index = 0;
for ($alpha = 0.0; $alpha <= 1.0; $alpha += 0.1) {
for ($beta = 0.0; $beta <= 1.0; $beta += 0.1) {
for ($gamma = 0.0; $gamma <= 1.0; $gamma += 0.1) {
$temp = $alpha + $beta + $gamma;
if ($temp != 1) {
continue;
}
echo "\n<p>$index: $alpha $beta $gamma. "</p>";

$index ++;
}}}

Output as follows:

0: 0 0.2 0.8

1: 0 0.3 0.7

2: 0 0.4 0.6

3: 0 0.5 0.5

4: 0 0.6 0.4

5: 0 0.7 0.3

6: 0 0.8 0.2

7: 0.1 0.1 0.8

8: 0.1 0.2 0.7

9: 0.1 0.3 0.6

10: 0.1 0.4 0.5

11: 0.1 0.5 0.4

12: 0.1 0.6 0.3

13: 0.1 0.7 0.2

14: 0.2 0 0.8

15: 0.2 0.1 0.7

16: 0.2 0.2 0.6

17: 0.2 0.3 0.5

18: 0.2 0.4 0.4

19: 0.2 0.5 0.3

20: 0.2 0.6 0.2

21: 0.2 0.8 0

22: 0.3 0 0.7

23: 0.3 0.1 0.6

24: 0.3 0.2 0.5

25: 0.3 0.3 0.4

26: 0.3 0.4 0.3

27: 0.3 0.5 0.2

28: 0.3 0.6 0.1

29: 0.3 0.7 0

30: 0.4 0 0.6

31: 0.4 0.1 0.5

32: 0.4 0.2 0.4

33: 0.4 0.3 0.3

34: 0.4 0.4 0.2

35: 0.4 0.5 0.1

36: 0.4 0.6 0

37: 0.5 0 0.5

38: 0.5 0.1 0.4

39: 0.5 0.2 0.3

40: 0.5 0.3 0.2

41: 0.5 0.4 0.1

42: 0.5 0.5 0

43: 0.6 0 0.4

44: 0.6 0.1 0.3

45: 0.6 0.2 0.2

46: 0.6 0.3 0.1

47: 0.6 0.4 0

48: 0.7 0 0.3

49: 0.7 0.1 0.2

50: 0.7 0.3 0

51: 0.8 0 0.2

52: 0.8 0.2 0
Feb 22 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
rf

<du*******@gmail.comwrote in message
news:eb**********************************@o77g2000 hsf.googlegroups.com...
>I want to use the following code to print all value of alpha + beta _
gamme = 1;

But I miss some of the results. I suspect PHP cannot run decimal loop
correctly. I should get 66 groups.
Floating point.

Not all decimal fractions can be expressed exactly in base two floating
point.

Discussed here and elsewhere many times.

--
Richard.
Feb 22 '08 #2

P: n/a
On Fri, 22 Feb 2008 10:27:08 +0100, du*******@gmail.com
<du*******@gmail.comwrote:
I want to use the following code to print all value of alpha + beta _
gamme = 1;

But I miss some of the results. I suspect PHP cannot run decimal loop
correctly. I should get 66 groups.

$index = 0;
for ($alpha = 0.0; $alpha <= 1.0; $alpha += 0.1) {
for ($beta = 0.0; $beta <= 1.0; $beta += 0.1) {
for ($gamma = 0.0; $gamma <= 1.0; $gamma += 0.1) {
$temp = $alpha + $beta + $gamma;
if ($temp != 1) {
An if ($temp != 1.0) would probably help to remove some quickyness.
continue;
}
echo "\n<p>$index: $alpha $beta $gamma. "</p>";

$index ++;
}}}
When working with floating points, don't look for equality, but for within
a range of error:
(1 - 0.05) < $temp < (1 + 0.05)

To avoid this, use integers where you can (I usually multply by powers of
10 untill satisfied with the accuracy).
--
Rik Wasmus
Feb 22 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.