4

How is it possible that TimeSpan with the same amount of tikcs gives different TotalDays values? Below is what i see on debuger just after running my app.

Ticks: 25920000000000
TotalDays: 30.0

enter image description here

After few more clicks when i hit the breakpoint at the same place the values looks like that:

Ticks: 25920000000000
TotalDays: 29.999999999999996

enter image description here

Since i used total days value later it hit me that once its 30 and the other time 29 for the 'same' TimeSpan Ticks ?!

This hapens only on my Corei7 (win7 x64) work station on 'AnyCpu' on .NET 3.5 Framework Configuration (on x86 is always 30, also on 3 other Work station 29 does not occurred). Any ideas?

pravprab
  • 2,301
  • 3
  • 26
  • 43
  • Try to be more specific when writing the title of the questions. – rebeliagamer Jan 31 '14 at 08:05
  • 1
    Are you using Direct3D? It is possible for the floating point precision to be modified at run-time. See this [question](http://stackoverflow.com/questions/21180461/can-something-in-c-sharp-change-float-comparison-behaviour-at-runtime-x64). – Mike Zboray Jan 31 '14 at 08:43

1 Answers1

1

Well, it's a double (so there's a conversion from a long -Ticks- to a double when you get TotalDays value), so you may have floating point precision "problems".

If you want an int, use TimeSpan.Days

TotalDays is a readonly property, which makes something like

return (double) this._ticks * (0.0 / 1.0);

(where _ticks is a long)

Days is a readonly proeprty also:

return (int) (this._ticks / 864000000000L);
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • `TimeSpan.Days` though would, as you say, only give the integer number of days... and in the OP's issue, the first one would return 30, but the second would be 29. – Chris Hammond Jan 31 '14 at 07:57
  • @ChrisHammond Days does not just "casting TotalDays to int" – Raphaël Althaus Jan 31 '14 at 07:59
  • You should remove `return (double) this._ticks * (0.0 / 1.0);` from your answer. That would obviously return zero every time. The correct code is `return (double)this._ticks * DaysPerTick;` where `DaysPerTick` is declared as `private const double DaysPerTick = 1.1574074074074074E-12;`. – Vegard Innerdal Feb 21 '14 at 16:24