I have been searching the internet trying to understand in its simplist form why this behavior happens.
Dim mysingle As Single = 456.11
Dim mybool As Boolean = mysingle = 456.11
In the lines above mybool becomes false. I found this behavior when putting the single into a double I found extra digits showing. The .net documentations states a single is an approimate value :S
I gatehr a single is a 32bit floating point number? But why are extra digits appearing when I have explicitly said what the number is.. surely the memory should store that numbers either side of my number are 0 to fill up the memory location?!
My brain is fried on this one :(
<> in VB.NET means "not equal to". It can be used with the normal oprands as well as in comparision with the items when compared with the datas fetched with the data reader (from database). Follow this answer to receive notifications.
Floating-point ( Single and Double ) numbers have larger ranges than Decimal numbers but can be subject to rounding errors. Floating-point types support fewer significant digits than Decimal but can represent values of greater magnitude.
The value being compared in the second statement isn't considered a Single
, rather it's being treated as a Double
. Since you're using VB.NET you can suffix it with a !
to force it to a Single
and this will return True
:
Dim mysingle As Single = 456.11
Dim mybool As Boolean = mysingle = 456.11!
I recommend reading: What Every Computer Scientist Should Know About Floating-Point Arithmetic.
It explains the issues with precision in floating point math in detail. For a simpler variation on the above, see The Floating Point Guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With