I'm performing some data type conversions where I need to represent uint
, long
, ulong
and decimal
as IEEE 754 double floating point values. I want to be able to detect if the IEEE 754 data type cannot contain the value before I perform the conversion.
A brute force solution would be to wrap a try-catch around a cast to double looking for OverflowException
. Reading through certain of the CLR documentation implies that some conversions just silently change the value without any exceptions.
Is there any fool proof way to do this check? I'm looking for completeness over ease of implementation. I have a feeling I'm going to be closely reading the IEEE 754 spec and checking matissa and exponent carefully...
I should add, that I am most concerned with representing whole numbers accurately and that loss of floating point precision is of secondary concern (but still worth considering).
EDIT: Int32 is able to be fully expressed as IEE-754. Also the Decimal
data type is very much part of the question.