Solved

'double' variable problem??????

Posted on 1998-08-12
15
207 Views
Last Modified: 2010-04-02
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
15 Comments
 

Author Comment

by:troy24
ID: 1170068
Edited text of question
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170069
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170070
>> when you subtract the time such as 1.15 to 2.00, you get 0. 45  but I want it to be .75
You aren't being consistent in your interpretatation.  If 1.00 represents an hour then 0.75 represents 3/4 of an hour or 45 minutes.  That is consistent.  But then 1.15 does not represent and hour an 15 minutes.  It represents and hour and 15/100 of an hour, about 9 minutes.  Thus
2.00 (2 hours) minus 1.15 is 0.85 or 85/100 of an hour or about 51 minutes, which is correct.

Thus you can do the math this way.  The integer portion is the number of whole hours and the decimal portion is the fractional part of an hour--not the minutes.  but there is a better way.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170071
What I would do is use a serial time for the math.  a serial time represents the time using a single number, unlike hours and minutes that use seconds.   The serial time scale starts at 0 at some time that you decide and then increases by one for each of the smallest units you want to measure.  Thus if you need accuaracty to the minute, you might start the serial time scale at 0 at midnight, then 1 would be 1 minute after midnight (12:01 PM).  Then 2 would be one minute later and so on. 1:15 PM, would then be 795 (13*60+15--more on that later)  If you wanted accuracy to the second, you would dot he same thing, but each successive number indicates an additional second, not a minute.

The advantage to the serial time is that you can do math using the ordinary +,- and even * and / operators.  Thus to find the difference between two serial times you can just subtract them with -.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170072
Now to make this work, you need to be able to convert between "ordinary" times and serial times--because no one wants to enter serial times into the computer.  But that is pretty easy.

Say you are doing it to the minute.  Then the formula is the
T = H*60 + M
where H is the hours (on a 24 hour clock) and M is the minutes.  For a second of accuracy you would use
T = H*60*60 + M *60 + S

To convert back you would use

H = Trunc(T / 60)
M = T - H*60

Let me know if you have questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170073
More time math.  I worked on an answer from 6:56 to 7:16.  that is 20 minutes.

If you don't like the answer or don't understand it you should at least post a comment saying why.

0
 
LVL 3

Expert Comment

by:elfie
ID: 1170074
you can check if the function difftime is available. in combination with mk_time, you create a solution with standard functions (at least on Unix)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Expert Comment

by:JYoungman
ID: 1170075
difftime() is always available, it's part of the C standard, which is a base document for the C++ standard.

0
 
LVL 2

Expert Comment

by:warmcat
ID: 1170076
troy, it's a dumb idea to use a floating point representation for what are inherently integer quantities.  Floating point arithmetic is inexact and will allow errors to creep into your accounting.  Seeing as you asked this in the C++ section, and sound like you want to roll your own, maybe you'd prefer to cook up a class which exhibits the behaviour you want using, say, an unsigned char each to store hours, minutes or seconds, and appropriate member functions to completely hide how the time is stored from the outside world.  If you use MFC, examine CTime.  And be a good chap, tell nietod what was up with his answer, because it looked fine to me.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170077
difftime uses serial times.  It just subtracts the two serial times and coverts the result to floating point.  The mktime() procedure is used to calculate the serial times it uses.  However that serial time can be used to calculate the difference in times between days.  That is, it is on a much longer scale.
0
 

Author Comment

by:troy24
ID: 1170078
Thanks for eveyones help. Sorry if I caused any problems but I am new at this.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1170079
Also do not do this:
double hours_worked;
if (hours_worked == 0.45) {
.
}

do not use ==, and != with doubles.  Because of rounding and inexactness you are likely to get some unexpected behavior
0
 
LVL 10

Accepted Solution

by:
rbr earned 20 total points
ID: 1170080
First don't use double with == and != as danny_pav already mentioned.
Second to calculated a hour fraction from minutes use the following formular

hour_fraction = min / 60 * 100

and the calculate the opposite direction.

min = hour_fraction *60 / 100

hope this helps.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170081
rbr, do you read these questions?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170082
troy, I spent a lot of time explaining this to you (for a measley 20 pts).  You rejected my answer without a word and then accepted rbr's "answer" which was a tiny subset of what I said.  with a littlle of what danny_pa said thrown in.  


0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now