473,769 Members | 3,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a very weird result

The code is as the following:

{
double limit= 0.02*33.00;
double limit1= 0.06*11.00;
double limit2= 0.03*22.00;
double limit3= 0.01*66.00;

limit=(floor(li mit*100))/100;
limit1=(floor(l imit1*100))/100;
limit2=(floor(l imit2*100))/100;
limit3=(floor(l imit3*100))/100;

printf("limit= %.2f\n",limit);
printf("limit1 = %.2f\n",limit1) ;
printf("limit2 = %.2f\n",limit2) ;
printf("limit3 = %.2f\n",limit3) ;
}

What's weird is the output:

limit= 0.66
limit1 = 0.65
limit2 = 0.65
limit3 = 0.66

Why are the result for limit1 and limit2 is 0.65 instead of 0.66?

Thanks in advance.
Jul 22 '05 #1
2 1464
ai lian wrote:

The code is as the following:

{
double limit= 0.02*33.00;
double limit1= 0.06*11.00;
double limit2= 0.03*22.00;
double limit3= 0.01*66.00;

limit=(floor(li mit*100))/100;
limit1=(floor(l imit1*100))/100;
limit2=(floor(l imit2*100))/100;
limit3=(floor(l imit3*100))/100;

printf("limit= %.2f\n",limit);
printf("limit1 = %.2f\n",limit1) ;
printf("limit2 = %.2f\n",limit2) ;
printf("limit3 = %.2f\n",limit3) ;
}

What's weird is the output:

limit= 0.66
limit1 = 0.65
limit2 = 0.65
limit3 = 0.66

Why are the result for limit1 and limit2 is 0.65 instead of 0.66?

Thanks in advance.


See eg.
http://www.petebecker.com/js200006.html

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
On 29 Jul 2004 11:29:37 -0700, ai lian <ai********@yah oo.com> wrote:
The code is as the following:

{
double limit= 0.02*33.00;
double limit1= 0.06*11.00;
double limit2= 0.03*22.00;
double limit3= 0.01*66.00;

limit=(floor(li mit*100))/100;
limit1=(floor(l imit1*100))/100;
limit2=(floor(l imit2*100))/100;
limit3=(floor(l imit3*100))/100;

printf("limit= %.2f\n",limit);
printf("limit1 = %.2f\n",limit1) ;
printf("limit2 = %.2f\n",limit2) ;
printf("limit3 = %.2f\n",limit3) ;
}

What's weird is the output:

limit= 0.66
limit1 = 0.65
limit2 = 0.65
limit3 = 0.66

Why are the result for limit1 and limit2 is 0.65 instead of 0.66?

Thanks in advance.


Rounding errors.

0.06*11.00*100 does not equal 66, obviously it equals a number very
slightly less than 66, floor then rounds that number down to 65 and so you
get 0.65.

Never assume that floating point arithmetic will give you exact results,
in particular your problem is that 0.06 is not *exactly* 6 one hundredths,
and the rounding errors start there.

john
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1791
by: Dominik B. | last post by:
I'm executing these commands: $text="example text"; $parts=explode(" ", $text); $result=$parts; $result becomes a weird array. echo "$result" prints 'example',
5
1688
by: redneck_kiwi | last post by:
All: I have an application that has been working well for 3-4 months now without any issues (that I am aware of). A day or two ago, one of our employees that use this application called to say she was getting a weird message: "Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/sys36/viewhist.php:2) in /www/htdocs/sys36/viewhist.php on line 5"
3
2079
by: redneck_kiwi | last post by:
Hi all: I have a really weird problem. I am developing a customer catalog system for my company and as such have delved into sessions for authentication and access levels. So far, I have managed to get a working system just about finished. I am building an interface for our customer service folks to use to manage registered customers and am seeing some weird behavior.
10
2166
by: Skybuck Flying | last post by:
This is the source code for nrand48 from gnuwin32 libgw32c long int nrand48 (xsubi) unsigned short int xsubi; { long int result; (void) __nrand48_r (xsubi, &__libc_drand48_data, &result); return result; }
1
7739
by: Dees | last post by:
Hi, I am facing a weird problem with HTTPS and Request.Url.AbsoluteUri in my ASP.NET application. Here is the scenario - 1. I have a menu page (Default.aspx), which has the following anchor - <a href="ApplicationHost.aspx">Open Application</a> 2. The ApplicationHost.aspx has the following code in its Load event handler - 'ASSUMPTION -
4
1402
by: Bo Peng | last post by:
Dear list, I spent the last 12 hours in catching this bug (?) and what I found out is very difficult to explain: Basically, I need to call a user-provided function many times, with a tuple as parameter. C/C++ side: (class A, constructed using a python function m_func)
12
2202
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify the most significant bit in a given element in an array of unsigned longs. Now I realise there may be a more efficient way to do this, and if you know a better way please let me know.
2
1621
by: jerrygarciuh | last post by:
Hi all, The following script is giving me weird problems. I have in this directory an index.php and hurricane.php. If the script gets $i = 'on' it is supposed to back up the current index into a file called normal.php and then copy hurricane.php into index.php. This should create a backup of the index and then put the hurrican alert in place.
7
2013
by: dtschoepe | last post by:
Hi, I am working on a project for school and I am trying to get my head around something weird. I have a function setup which is used to get an input from stdin and a piece of memory is created on the heap using malloc. This input is then run through a regex and another test. The result is passed back as a char pointer to the program for further processing The first time I call this function, it performs normally. However,
16
1774
by: Bill Nguyen | last post by:
I'm running into a very weird problem regarding subtraction. Subtraction behaves as if it's an addition in the below sub txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0") txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")
0
9579
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, 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...
0
9416
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,...
0
9850
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 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...
0
8862
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, 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...
1
7396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
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();...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
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
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.