need understand why getting time differences. here vba code:
public ohlcarray(1 481, 0 28, 0 3) single 'debug stmt below dim debugtime double ohlcarray(1, 0, 3) = now() 'next 3 lines debugging lines debugtime = ohlcarray(1, 0, 3) print #1, "now()=" & debugtime print #1, & "debug print line - (just before pushing"
here debug output lines in logfile:
now()=42527.4609375 06/06/2016 11:01:00 amdebug print line - (just before pushing
just know, time of 0.4609375 = 11:03:45 am; expecting in second print statement show me this, or off no more 2 seconds or so.
my questions: in above example, why getting different times? there difference between , now()? can affect capturing of time, seems show above?
thanks help. karl.
p.s. here 2 debug output lines before lines identified above:
06/06/2016 10:59:34 - skipped ws_calculate routine - currenttimeid processed - currenttimeid = 45 06/06/2016 10:59:54 - skipped ws_calculate routine - currenttimeid processed - currenttimeid = 45
p.s.s. here link related topic - not sure if applies issue: postgres now() vs 'now' in function
p.s.s. vegard. upon reviewing other log files similar output data, can determined confusion arose result of 2 errors: a) did not reset debugtime 0 before moving new single data (where smaller fractional digits come when single overlaid double?), , b) observation, transferring of single data type double data type wasn't useful since truncation of full date data occurred when moved array defined single. think maintain array single, however, avoid doubling in size, when 99.8% of array works fine defined single. can accept time being off 2 minutes in headers of sheet (when array transferred it). have learned moving date/time data data type of single in applications inadequate. data type should double or datetime.
p.s.s. final: logic ran today (wednesday, near 11am) debug statements showed me following: ohlcarray(1, 0, 3) = now()
ohlcarray element being single captured date.time 42529.46. now() 42529.45905067 (equivalent 11:01:02am), , appeared in print.debug , print log file (correctly). when casted, rounded .46.
debugtime = ohlcarray(1, 0, 3)
when array element casted debugtime, additional digits picked (erroneously). debugtime = 42529.46093750. translates 11:03:45am. here gets interesting. when move array element (42529.46) later excel sheet (a sheet reflects complete array) - cell formatted display number time only, 11:03:45am in sheet. means cell in sheet receiving number in same way statement debugtime = ohlcarray(1, 0, 3). because remember, array element 42529.46. 0.46 converts 11:02:24am.
thanks vegard effort. hope follow helps give more insight of not obvious ways excel , vba behave. tell non-computer literate friends: "the computer never wrong!"
any instance use of single
in context of dates gives correct answer extreme coincidence.
to further illustrate why, building on gary's answer, i've reversed code array declaration double
- makes easier when want compare correct double
versus single
-cast-to-double
not correct.
sub testtime() dim ohlcarray(1 481, 0 28, 0 3) double 'debug stmt below dim dbltime double, sngtime single ohlcarray(1, 0, 3) = now() 'next 3 lines debugging lines dbltime = ohlcarray(1, 0, 3) sngtime = ohlcarray(1, 0, 3) debug.print "doubletime: " & dbltime debug.print "singletime: " & sngtime debug.print " date double single double(single2)" debug.print "singletime = " & cdate(sngtime) & " - " & cdbl(sngtime) & " - " & csng(sngtime) & " - " & cdbl(sngtime2) debug.print "doubletime = " & cdate(dbltime) & " - " & cdbl(dbltime) & " - " & csng(dbltime) debug.print "now() = " & now() & " - " & cdbl(now()) & " - " & csng(now()) debug.print "now = " & & " - " & cdbl(now) & " - " & csng(now) end sub
now, let's review output. i've run several times, , illuminates issue. observe happens (or rather, doesn't happen) in first row of column "double", cast single double, , compare correct double
on next row:
doubletime: 42528,3991666667 singletime: 42528,4 date double single singletime = 07.06.2016 09:33:45 - 42528,3984375 - 42528,4 doubletime = 07.06.2016 09:34:48 - 42528,3991666667 - 42528,4 now() = 07.06.2016 09:34:48 - 42528,3991666667 - 42528,4 = 07.06.2016 09:34:48 - 42528,3991666667 - 42528,4 doubletime: 42528,3995138889 singletime: 42528,4 date double single singletime = 07.06.2016 09:33:45 - 42528,3984375 - 42528,4 doubletime = 07.06.2016 09:35:18 - 42528,3995138889 - 42528,4 now() = 07.06.2016 09:35:18 - 42528,3995138889 - 42528,4 = 07.06.2016 09:35:18 - 42528,3995138889 - 42528,4 doubletime: 42528,3996180556 singletime: 42528,4 date double single singletime = 07.06.2016 09:33:45 - 42528,3984375 - 42528,4 doubletime = 07.06.2016 09:35:27 - 42528,3996180556 - 42528,4 now() = 07.06.2016 09:35:27 - 42528,3996180556 - 42528,4 = 07.06.2016 09:35:27 - 42528,3996180556 - 42528,4 doubletime: 42528,3998726852 singletime: 42528,4 date double single singletime = 07.06.2016 09:33:45 - 42528,3984375 - 42528,4 doubletime = 07.06.2016 09:35:49 - 42528,3998726852 - 42528,4 now() = 07.06.2016 09:35:49 - 42528,3998726852 - 42528,4 = 07.06.2016 09:35:49 - 42528,3998726852 - 42528,4 doubletime: 42528,4045486111 singletime: 42528,41 date double single singletime = 07.06.2016 09:45:00 - 42528,40625 - 42528,41 doubletime = 07.06.2016 09:42:33 - 42528,4045486111 - 42528,41 now() = 07.06.2016 09:42:33 - 42528,4045486111 - 42528,41 = 07.06.2016 09:42:33 - 42528,4045486111 - 42528,41
note date conversions using single
, timestamp static extended periods of time because datatype can't support sufficient digits.
the fact casting double
produces more digits doesn't make right -- becomes more accurate, still not correct.
so problem not seem now
or now()
! shown in examples, long correct datatype used, both yield correct result.
your first problem, incorrect timestamps, caused array being single
instead of double
.
your second problem else in entirely, perhaps in code. either overloading now
or perhaps more probable, print
doing funky @ runtime. try prefacing print
statement debug.print "now: " & now
, compare result logfile, or better yet, pursuant documentation, change this:
print #1, & "debug print line - (just before pushing"
to this:
print #1, cdate(now) & "debug print line - (just before pushing"
date data written file using standard short date format recognized system. when either date or time component missing or zero, part provided gets written file.
edit:
as mysterious digits appear when casting single
double
, not approximation done compiler, mistakenly guessed earlier. known side-effect of how numbers represented, apparently common topic in computer science.
for numeric precision, use decimal datatype.
msdn talks floating-point precision in visual basic context in detail.
here: https://msdn.microsoft.com/en-us/library/system.double.aspx?cs-save-lang=1&cs-lang=vb#conversions
type conversion. double structure provides explicit interface implementation iconvertible interface, supports conversion between 2 standard .net framework data types. language compilers support implicit conversion of values of other standard numeric types double values. conversion of value of standard numeric type double widening conversion , not require user of casting operator or conversion method.
however, conversion of int64 , single values can involve loss of precision.
the problem of precision affects single values converted double values.
and here: https://msdn.microsoft.com/en-us/library/system.single.aspx
for example, 2/10, represented precisely .2 decimal fraction, represented .0011111001001100 binary fraction, pattern "1100" repeating infinity. in case, floating-point value provides imprecise representation of number represents. performing additional mathematical operations on original floating-point value increases lack of precision. example, if compare results of multiplying .3 10 , adding .3 .3 9 times, see addition produces less precise result, because involves 8 more operations multiplication. note disparity apparent if display 2 single values using "r" standard numeric format string, which, if necessary, displays 9 digits of precision supported single type.