Tasos
Tasos

Reputation: 7577

0.9 < 0.9 is True in vb.net

I am working on a VB.Net project. Somewhere on the solution, I have this part of the code:

Dim my_variable As Single = 1

'other code goes here

If do_some_tests_here Then
   my_variable = 0.9
End If

If my_variable < 0.9 Then
   'do some other stuff here
End If

I realized that when the my_variable gets into the first If and changes it value to 0.9, then the second condition my_variable < 0.9 returns True and the code inside is executed.

I have read the comparing float numbers problems and that you should avoid it, but what is an alternative solution to the above?

Upvotes: 0

Views: 132

Answers (2)

Simon Byrne
Simon Byrne

Reputation: 7864

The literal 0.9 isn't exactly 0.9, but instead is the closest Double value, which is 0.90000000000000002220446049250313080847263336181640625.

On the other hand, the closest Single value is 0.89999997615814208984375, which is less than the Double value.

Upvotes: 2

DaanV
DaanV

Reputation: 339

The problem doesn't seems to arise if you change the variable from Single to Double. I think the compiler is turning the 0.9 into a double itself. but you can also turn the 0.9 into a single by using:

If my_variable < CSng(0.9) Then

or you can tell the compiler that it is a single by using the Letter F (https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/type-characters)

If my_variable < 0.9F Then

Upvotes: 7

Related Questions