math - Why should I use t1 - t0 < 0, not t1 < t0, when using System.nanoTime() in JAVA -
when reading system.nanotime() api in java. found line:
one should use t1 - t0 < 0, not t1 < t0, because of possibility of numerical overflow.
http://docs.oracle.com/javase/7/docs/api/java/lang/system.html#nanotime()
to compare 2 nanotime values
long t0 = system.nanotime(); ... long t1 = system.nanotime();
one should use t1 - t0 < 0, not t1 < t0, because of possibility of numerical overflow.
i want know why t1 - t0 < 0
preferable way prevent overflow.
because read other thread a < b
more preferable a - b < 0
.
java integer compareto() - why use comparison vs. subtraction?
these 2 things make contradiction.
the nano time not 'real' time, counter increments starting unspecified number when unspecified event occurs (maybe computer booted up).
it overflow, , become negative @ point. if t0
before overflows (i.e. large positive), , t1
after (very large negative number), t1 < t0
(i.e. conditions wrong because t1
happened after t0
).....
but, if t1 - t0 < 0
, well, magic same overflow (undeflow) reasons (very large negative subtract large positive underflow), result number of nanoseconds t1 after t0
..... , right.
in case, 2 wrongs make right!
Comments
Post a Comment