Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,000 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,327 people online right now. Registration is fast and FREE... Join Now!




Strange number appearing after calculation

 
Reply to this topicStart new topic

Strange number appearing after calculation, Program will not recognize 0 (zero)

grinsalot
1 Dec, 2006 - 01:58 PM
Post #1

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 50


My Contributions
Hi,

I've created a simulation program. The program is not recognizing zero for some reason. For each pyramid enqueue and bus dequeue the program will subtact .2 from the total num_of_cycles. For example:

CODE
  if(num_of_cycles <= bus_queue.bus_queuecount() * load_offload_time_per_cust && num_of_cycles > 0)
    {
      while(num_of_cycles > 0)
        {
          bus_queue.dequeue(i);
          pyramid_queue.enqueue(i);
          unloaded_tourists_at_pyramids++;
          total_tourists_transported_to_pyramids++;
          num_of_cycles = num_of_cycles - load_offload_time_per_cust;
        }
    }


My num_of_cycles variable will calculate down to 0.2. The next number should be 0, because the load_offload_time_per_cust is set at 0.2. However, instead the program calculates this subtraction as 1.13076e-13. All of the prior calculations are correct. It's this final one that just won't compute correctly. Am I missing something?

Thank you in advance.

User is offlineProfile CardPM
+Quote Post

ifoam
RE: Strange Number Appearing After Calculation
1 Dec, 2006 - 02:08 PM
Post #2

D.I.C Head
**

Joined: 26 Oct, 2006
Posts: 54



Thanked: 1 times
My Contributions
make sure you initialize your variables before using them.

sometimes if you don't, strange values will be left in place in memory.


User is offlineProfile CardPM
+Quote Post

grinsalot
RE: Strange Number Appearing After Calculation
1 Dec, 2006 - 02:12 PM
Post #3

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 50


My Contributions
QUOTE(ifoam @ 1 Dec, 2006 - 03:08 PM) *

make sure you initialize your variables before using them.

sometimes if you don't, strange values will be left in place in memory.

Hi ifoam,

Yes, I've initialized the variables, so that's not the problem. Thank you though.
User is offlineProfile CardPM
+Quote Post

ifoam
RE: Strange Number Appearing After Calculation
1 Dec, 2006 - 02:17 PM
Post #4

D.I.C Head
**

Joined: 26 Oct, 2006
Posts: 54



Thanked: 1 times
My Contributions
i would try:
CODE
if(num_of_cycles=.2)
num_of_cycles = 0;
else
num_of_cycles = num_of_cycles - load_offload_time_per_cust;


see if works like that

This post has been edited by ifoam: 1 Dec, 2006 - 02:18 PM
User is offlineProfile CardPM
+Quote Post

grinsalot
RE: Strange Number Appearing After Calculation
1 Dec, 2006 - 02:33 PM
Post #5

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 50


My Contributions
QUOTE(ifoam @ 1 Dec, 2006 - 03:17 PM) *

i would try:
CODE
if(num_of_cycles=.2)
num_of_cycles = 0;
else
num_of_cycles = num_of_cycles - load_offload_time_per_cust;


see if works like that

Thank you ifoam - that worked. I still don't get it, though. I declared the variable load_offload_time_per_cust as a constant variable. Num_of_cycles counts down until .2 and then goes crazy, thus driving me crazy.

Thank you, again for your help.
User is offlineProfile CardPM
+Quote Post

ifoam
RE: Strange Number Appearing After Calculation
1 Dec, 2006 - 02:39 PM
Post #6

D.I.C Head
**

Joined: 26 Oct, 2006
Posts: 54



Thanked: 1 times
My Contributions
that's strange. maybe someone here has an answer..
User is offlineProfile CardPM
+Quote Post

grinsalot
RE: Strange Number Appearing After Calculation
2 Dec, 2006 - 05:58 AM
Post #7

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 50


My Contributions
QUOTE(ifoam @ 1 Dec, 2006 - 03:39 PM) *

that's strange. maybe someone here has an answer..

Problem Found.

I added a cout statement and allowed output to 100 places after the decimal point. This is what I found:

> 3.0000000000001136868377216160297393798828125
> 2.80000000000011350920203767600469291210174560546875
> 2.80000000000011350920203767600469291210174560546875


Even though I initialized all of my variables, I still have some garbage output. It was suggested to me that I use ints instead of doubles and divide my final answers by 10 to achieve the same results without the garbage output.

I wasn't even aware that this problem could arise. I thought the point of initializing was to prevent this kind of surprise!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 08:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month