# Problems with Time

(Andy Jones) #1

Another one for you folks. Can anyone tell me what is going on here?

360.upto(370) do |d|
time = Time.now
t = time - (d * 60 * 60 * 24)
puts "%s: %s | %s" % [t, time - t, Time.now - t]
end

Output:

2018-04-29 14:53:06 +0100: 31104000.0 | 31104000.00043
2018-04-28 14:53:06 +0100: 31190400.0 | 31190400.000098
2018-04-27 14:53:06 +0100: 31276800.0 | 31276800.000087
2018-04-26 14:53:06 +0100: 31363200.0 | 31363200.000066
2018-04-25 14:53:06 +0100: 31449600.0 | 31449600.000183
2018-04-24 14:53:06 +0100: 31536000.0 | 31536000.000071
2018-04-23 14:53:06 +0100: 31622400.0 | 31622400.000084
2018-04-22 14:53:06 +0100: 31708800.0 | 31708800.000066
2018-04-21 14:53:06 +0100: 31795200.0 | 31795200.000064
2018-04-20 14:53:06 +0100: 31881600.0 | 31881600.000088
2018-04-19 14:53:06 +0100: 31968000.0 | 31968000.000065

I'd expect `time - t` to be slightly different from `Time.now - t`, of course, but why does it appear to have different precision? `Time.now`, `time` and `time - t` are all of class Time, so there's nothing funny there?

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.

(John W Higgins) #2

What would you expect here?

Another one for you folks. Can anyone tell me what is going on here?

360.upto(370) do |d|

time = Time.now

t = time - (d * 60 * 60 * 24)

puts "%s: %s | %s" % [t, time - t, Time.now - t]

end

Output:

2018-04-29 14:53:06 +0100: 31104000.0 | 31104000.00043

You take a time - subtract an exact amount of seconds - and subtract - you
get a Float with no nanoseconds because they have the same nanoseconds
field.

You then take 2 times with different nanosecond fields - and you subtract
then - the nanoseconds fields are different.

This is not a precision issue - it's an input issue (if one wishes to even
call it an issue).

John

P.S. You are being way too fancy here for your own good. t = time - 1 is
where you start here - throwing a bunch of this or that in the middle of it
just makes the problem very much more obfuscated. You subtract one second
and then ask for the number of seconds between the times you get 1.0 - you
subtract 1.00000312731 seconds and ask for the different you get
1.00000312731 seconds

···

On Wed, Apr 24, 2019 at 7:00 AM Andy Jones <Andy.Jones@jameshall.co.uk> wrote:

(Carlo E. Prelz) #3

Quoting Andy Jones (Andy.Jones@jameshall.co.uk):

I’d expect `time – t` to be slightly different from `Time.now – t`,
of course, but why does it appear to have different precision?–
`Time.now`, `time` and `time – t` are all of class Time, so there–s
nothing funny there?

The precision is the same. But when you print 'time-t', you actually
print

time-(time-(d*60*60*24))

you actually print d*60*60*24 , which is integer, although stored in a
floating-point variable.

What you see in 'Time::now-t' is slightly larger, and this amount is
seen in the decimals (my differences are much smaller than yours...).

Carlo

···

Subject: Problems with Time
Date: mer 24 apr 19 02:00:09 +0000

--
* Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

(Andy Jones) #4

You take a time - subtract an exact amount of seconds - and subtract - you get a Float with no nanoseconds because they have the same nanoseconds field.

Oh FFS. Thank you. It’s that old magic trick, isn’t it? “Now take away the number you first thought of”.

This is not a precision issue - it's an input issue (if one wishes to even call it an issue).

I am the issue in this case…

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.