any .net gurus want a tricky question?

antic

Perch
Hey all,

I got this seriously twisted exception in VB.NET. Never seen this before...

I have a variable (Single precision) which contains the results of a calculation. The result is 75.20834 when I print it out.

But when I try to assign it to an integer as follows, I get an "overflow" exception!

intResult = CInt(sngResult)

intResult is an Integer, sngResult is a Single.

However, when I do something like Response.Write CInt(sngResult), it works fine!! Assigning CInt(sngResult) to an Integer variable is giving me an overflow error!

Anyone know why this is? Drove me nuts until I accepted it was a .net quirk that I didn't fully understand.
 
Well that's very strange. I just fired up VS 2005 and put this in a button click

Dim x As Single
Dim y As Integer

x = 75.20834
y = CInt(x)
MsgBox(y)

That works and happily says 75.

Perhaps you should try inserting the line:

sngResult = 75.20834

before the CInt. If that works then it might prove that there is something wierd about the value of sngResult, like it has extra 'invisible' digits or something. Not that it necessarily helps solve the problem but it might get you a bit closer.

Cheers
Ross
 
Thanks for the reply, but now I'm kinda embarassed... turned out to be one of problems which is inside a loop, so whole set of values are calculated during the period of the loop... one of those values ended up being NaN - that was causing the CInt to throw the exception.

I stupidly put my debug output in the loop, so I only ended up outputting the FIRST value: 75.20834. :) As you can guess, it was a later value which ended up being NaN and caused the problem.

Once I realised that, I felt a bit silly. :D

Thanks for going to the trouble, sorry it was a wild good chase (me being the goose)!
 
Back
Top