Home All Groups Group Topic Archive Search About

Floating Point Math Problem

Author
8 Jul 2009 12:51 AM
Joe
I am attempting to solve a simple floating point math problem
and I am not getting the results that I think I should be getting.
Here is what I am doing:

Dim iMinutes As Integer
Dim dHours, dMinutes, dUnits As Double

            iMinutes = DateDiff(DateInterval.Minute, dStartTime,
dEndTime)
            dHours = iMinutes \ 60
            dMinutes = iMinutes Mod 60
            dUnits = dHours + (dMinutes \ 60.0)

My input start time is 9:00AM and my ending time is 10:15AM

dHours = 1   (good)
dMinutes = 15  (good)
dUnits = 1   (not good, should be 1.25)

Can someone tell me what I am doing wrong?   It seems like I cannot
get any numbers to the right of the decimal point for dUnits.

Author
8 Jul 2009 1:32 AM
Stephany Young
You're NOT using the divide operator for floating point math.

You're using the division operator for integer math.

Use the / operator instead of the \ operator.


Show quoteHide quote
"Joe" <delphi***@cox.net> wrote in message
news:c2e7a6ac-79f3-43ee-8d47-c4680d3b1d04@g31g2000yqc.googlegroups.com...
>        I am attempting to solve a simple floating point math problem
> and I am not getting the results that I think I should be getting.
> Here is what I am doing:
>
> Dim iMinutes As Integer
> Dim dHours, dMinutes, dUnits As Double
>
>            iMinutes = DateDiff(DateInterval.Minute, dStartTime,
> dEndTime)
>            dHours = iMinutes \ 60
>            dMinutes = iMinutes Mod 60
>            dUnits = dHours + (dMinutes \ 60.0)
>
> My input start time is 9:00AM and my ending time is 10:15AM
>
> dHours = 1   (good)
> dMinutes = 15  (good)
> dUnits = 1   (not good, should be 1.25)
>
> Can someone tell me what I am doing wrong?   It seems like I cannot
> get any numbers to the right of the decimal point for dUnits.
>
Author
8 Jul 2009 2:26 AM
Family Tree Mike
Joe wrote:
Show quoteHide quote
>         I am attempting to solve a simple floating point math problem
> and I am not getting the results that I think I should be getting.
> Here is what I am doing:
>
> Dim iMinutes As Integer
> Dim dHours, dMinutes, dUnits As Double
>
>             iMinutes = DateDiff(DateInterval.Minute, dStartTime,
> dEndTime)
>             dHours = iMinutes \ 60
>             dMinutes = iMinutes Mod 60
>             dUnits = dHours + (dMinutes \ 60.0)
>
> My input start time is 9:00AM and my ending time is 10:15AM
>
> dHours = 1   (good)
> dMinutes = 15  (good)
> dUnits = 1   (not good, should be 1.25)
>
> Can someone tell me what I am doing wrong?   It seems like I cannot
> get any numbers to the right of the decimal point for dUnits.
>

I think that dEndTime.Subtract(dStartTime).TotalHours is simpler.

--
Mike