473,394 Members | 1,935 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,394 software developers and data experts.

Strange for loop

1
Hello guys, I got a strange problem when using for loop.

For example, the following code

for ($j = 0.0; $j <= 2; $j+=0.2) {
print $j, " ";
}

gives
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 ,
which is normal since the loop is executed 11 times as expected.

However, the following code

for ($j = 0.0; $j <= 3; $j+=0.2) {
print $j, " ";
}

gives
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 ,
where the loop is executed only 15 times instead of expected 16 times!

I can't figure out why, any help is appreciated, thanks!




:confused: :confused:
Nov 3 '05 #1
1 1611
for ($j = 0.0; $j <= 3; $j+=0.2) {
print $j, " ";
}
First of all, use "." (dot) in stead of "," (comma) for string concat.
Second, you're using floating point variables. Read a bit about them here:
http://www.php.net/manual/en/language.types.float.php

There's a pretty pink warning box which clearly says never to compare floating point numbers for equality (directly or indirectly, for example using ($i < $j) || ($i > $j) in stead of ($i != $j) is STILL wrong).
Dec 6 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: hall | last post by:
Hi. I've come across someting strange. I was trying to make a for-loop execute repetadly until the function called inside it does not return true during the entire loop (see program below). ...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
2
by: TB | last post by:
I am seeing a very strange problem as follows... I have a loop where a fair amount of processing is going on and near the top of the loop I access a class that has only static helper functions...
5
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I...
20
by: SpreadTooThin | last post by:
I have a list and I need to do a custom sort on it... for example: a = #Although not necessarily in order def cmp(i,j): #to be defined in this thread. a.sort(cmp) print a
4
by: | last post by:
Im getting that error (It is strange. I Run my programme step by step pressing f11. im looping SqlCommand in a while loop. The First step runs but when a enter 2nd step in my loop it returns error...
11
by: VijaKhara | last post by:
Hi all, I just write a very simple codes in C and vthere is a very strange bug which I cannot figure out why. The first loop is for v, and the second for k. There is no relationship between v...
3
by: Rinaldo | last post by:
Hi, I have a label on my dialogbox who has to change text while running. This is what I do: lblBackup.Text = "Bezig met de backup naar " + F1.FTPserver; but the text does'nt appear, only if...
6
by: markus.litz | last post by:
Hello, I have a strange problem with some of my c++ code. I have a normal for- loop like this for(int i = 1; ..... on MS-Windows with a Microsoft compiler everything is alright, but when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.