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:
1 1634
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).
Sign in to post your reply or Sign up for a free account.
Similar topics |
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).
The two lines that confuse me are marked as (1) and (2).
count=0;
bool s(true);
|
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 across some wierd loop behavior. With the
pasted code I receive the output that follows.
I realize that the code is broken, because the inner loop fails to reset
j for each iteration of the outer loop (the fix is commented out). I
also know that...
|
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 to perform some calculations. After some number of
iterations, randomly, I'll get an uncaught NullValueException error on
one of these calls, as if the class name is being treated as an object
reference and is null. Here is some psuedo-code to...
|
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 have no
clue why.
But if I store the MethodInfo in a local variable I works as expected.
I do not understand why this is so, shouldn't both ways be semantically
equal?
|
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
| |
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 ? )
any idea ?
System.Data.SqlClient.SqlException: Procedure or function sp_adduser has
too many arguments specified
My Stored Procedure
|
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 and k but if I debug and watch the change of
the variable after each command.
When the sencond loop happends for k, the values of vs change
and are set to be equal some values of k. Specifically, v is
|
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 I draw (call) a messagebox, then the
text appears. What can it be? I had made a total new one, but did'nt help.
|
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 compile the same source on Linux with gcc-3.4, the variable "i"
is "0" in the first pass of the loop.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |