Click here to Skip to main content
15,891,694 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I generally like to use Strict On for my programs. Is there any way to tell VB.net (I presently use 2005, but would be curious about later versions) that many types of implicit narrowing conversions should be regarded as errors, but double->single conversions should simply be done silently? It is rather annoying having to cast the parameters to graphics routines, and I'm really not clear why such casts are necessary. Will conversion from double to single ever throw an exception? Out-of-range values yield 1.#INF, so I can't think of any 'exception' case.

If double->single is considered a narrowing conversion because of a potential loss of precision, why would not integer->single or long->double be considered likewise, given that csng(&h12345678) is &h12345680 and cdbl(&h1234567890ABCDEF) is 1234567890ABCE00 (both clearly losing precision)?
Posted
Updated 24-Mar-10 7:04am
v2

1 solution

In a nutshell, no, there is no way to do this.


supercat9 wrote:
It is rather annoying having to cast the parameters to graphics routines, and I'm really not clear why such casts are necessary.


If it's annoying, turn the Strict option off. With Strict on, your basically being forced to tell the compiler that you know the conversion can result in data loss.


supercat9 wrote:
Will conversion from double to single ever throw an exception


No. If, while using Convert.ToSingle, the value of the Double is outside the range of values supporting by Single, the resulting Single will have the value (+/-)Infinity.


supercat9 wrote:
If double->single is considered a narrowing conversion because of a potential loss of precision, why would not integer->single or long->double be considered likewise?


Because the range of values supported by Integer and Long are well within the range of values supported by Single and Double. There is no loss of precision when converting from an Integer to Single or Long to Double.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900