?
Solved

trouble with 'double' in Visual C++ 5.0

Posted on 1998-08-12
2
Medium Priority
?
189 Views
Last Modified: 2013-11-20
I am creating a service report and the users have to enter in the time they started
   to the time they finished and I want to calculate the amount of hours they have
   worked.  The method I am using seems to work the odd time but I am having
   trouble getting it to work all the time.  I am using double variables and I am not
   sure if I should be using them. I am also changing the time into 24 hr clock if the
   starttime is larger than the endtime.
   eg. starttime  11.00  endtime 2.00->14.00
   This works all the time but it is when you subtract the time such as 1.15 to 2.00,
   you get 0. 45  but I want it to be .75 because you have worked 3/4 of an hour.  I
   am only doing it in 15 minute intervals so I didn't think it woul be that difficult.  This
   is how I am doin it.
   if (m_hourswork==0.45) //45 minutes worked
       m_hourswork=0.75;
   if m_hourswork==0.15)  //15 minutes worked
      m_hourswork=0.25)
   I do this for all 15 min intervals and every hour upto 10 hours because no one will
   work more than 10.  The method I am using seems to work from time to time so I
   think it may be the 'double' varibles I am using.

   Thank You for the help!!
   Greatly Needed
0
Comment
Question by:troy24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
jstolan earned 40 total points
ID: 1320642
The problem is with rounding in the double numerical system.  Since doubles are binaries, they cannot exactly represent all decimal numbers.  If you really want to do it this way, you need to allow for this.  for example your first test for the 45 minutes could be expressed as:

if( ( m_hourswork - 0.45 ) < 0.001 )   // 45 minutes worked
    m_hourswork = 0.75.

I must say though that this is a really clumsy way of approaching your problem.  MFC has some wonderful time handling functions.  Why don't you check the CTime class?
0
 

Author Comment

by:troy24
ID: 1320643
Thank you very much for the help, I am going to try it in a few minutes.
I will also look at the CTime class.
Thanks again.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question