Why doesn't MOD return the correct value?

Why does this code not work

[code] dim d as new date

	MsgBox str( d.TotalSeconds mod 60 )  // Displays "-8" incorrectly[/code]

While this code does

[code] dim d as new date

	MsgBox str( 3569404548 mod 60 ) // Displays "48" correctly [/code]

Is there something funny about d.TotalSeconds being a double?

(Yes, I know you can use d.second but I’m more curious as to why the above fails.)

What if you use mod 60.0?

That’s because of d.TotalSecond is converted into Int32 before doing modulo operation. The conversion causes integer overflow. So, the result obviously would be wrong.

Try this

dim d as new Date
dim tsd as Double = d.TotalSeconds
dim tsu as UInt64 = tsd
dim tsi as Int64 = tsd
dim tsi32 as Int32 = tsd

MsgBox Str(tsd mod 60)
MsgBox Str(tsu mod 60)
MsgBox Str(tsi mod 60)
MsgBox Str(tsi32 mod 60)

This is exactly it. If you execute:

msgbox str(ctype(d.totalseconds, integer))

You’ll see the negative value due to the 32-bit integer overflow. But if you explicitly cast to int64 it will work.

MsgBox str( ctype(d.TotalSeconds, int64) mod 60 ) 

Interesting! Thank you everyone.