Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple Arithmetic?

47 views
Skip to first unread message

Bob Darlington

unread,
May 2, 2013, 10:06:07 PM5/2/13
to
I'm posting this with an uneasy feeling that I'm missing something very
obvious, but here goes anyway.

In the following code, vChildCostOff and vChildCostOn are dates and
vCoreHours is a double.
The dates are only ever even hours or half hours.
The following debug.print lines return the 3 lines below.

Debug.Print vChildCostOff, vChildCostOn, vCoreHours
Debug.Print 24 * (vChildCostOff - vChildCostOn)
Debug.Print (24 * (vChildCostOff - vChildCostOn)) - vCorehours

5:30:00 PM 8:30:00 AM 9
9
-1.33226762955019E-15

Can anyone please explain why the final line doesn't return a zero?

--
Bob Darlington
Brisbane


David Hare-Scott

unread,
May 2, 2013, 11:02:30 PM5/2/13
to
Because floats and doubles don't necessarily have an exact value like ints
and longs, it is the nature of the beast. For example, when doing floating
point arithmetic sometimes you get approximations like 0.299999999999999999
when you expected to get 0.3

~E-15 is as close to zero as floating point arithmetic can get in this case.
You need to do some rounding.

David

Bob Darlington

unread,
May 3, 2013, 12:15:25 AM5/3/13
to
"David Hare-Scott" <sec...@nospam.com> wrote in message
news:klv9bb$k79$1...@news.albasani.net...
Thanks David.
I must have been lucky in the past, because I've never encountered the
problem before.
Anyway, I've already started working through my code and rounding where
required.

--
Bob Darlington
Brisbane


Phil

unread,
May 3, 2013, 4:16:10 AM5/3/13
to
If both times and core hours are multiples of 1/2 hours (or 1/4 hours), why
not convert them to integers so that 5:30 PM becomes 70, 8:30AM becomes 34
and 9 hours becomes 36 (based on 1/4 hour units)

Phil

Ron Paii

unread,
May 3, 2013, 8:19:43 AM5/3/13
to


"Bob Darlington" <b...@notheredpcman.com.au> wrote in message
news:51831b1d$0$39044$c3e8da3$dbd...@news.astraweb.com...
Try using date functions, direct arithmetic on dates can return unexpected
results.

datediff("h", #8:30:00 AM#, #5:30:00 PM# )
returns integer 9
datediff("h", #8:30:00 AM#, #5:30:00 PM# ) - 9
returns integer 0

ps
format(-1.33226762955019E-15, "0.000000000000000")
returns -0.000000000000001


Bob Darlington

unread,
May 7, 2013, 12:39:32 AM5/7/13
to
Thanks Ron & Phil.
I need to end up with a number of hours (to 1 dec place), so all I needed to
do was to round the result using ( ..* 10) /10 .
All is well now, but thanks for your help.

--
Bob Darlington
Brisbane
"Ron Paii" <No...@none.com> wrote in message
news:km09qk$mr6$1...@dont-email.me...
0 new messages