473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
2 4845
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
4
by: David Veeneman | last post by:
A project that I have been working on has stopped executing loops correctly. I can add a simple loop to a class in the project, like this: for (int i = 0; i < 5; i++) { Console.Write(i); } ...
3
by: Brenny | last post by:
Hello I'm developing a project by using access database and there is a table(Table1) and it has a field(ml_mik) this is the decimal field. I'm taking a value from windows form as a decimal...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
3
by: ajaksu | last post by:
Hello c.l.p.ers :) Running long(Decimal) is pretty slow, and the conversion is based on strings. I'm trying to figure out whether there is a good reason for using strings like in decimal.py...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
7
by: billbaitsg | last post by:
Hi, I need some help with figuring out why my program is all messed up. There are two portions two it, one that converts from roman to decimal (rom2dec) and another that converts from decimal to...
10
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.