M
Mike S
Does anyone know the logic behind why in VB.NET the result of a
floating-point division ('/') is -rounded- on being converted to an
integer type, such as with statements like
Dim x As Integer = 2/3 'after assignment, x is 1, whereas a sane person
would say it should be 0
Does Microsoft have a reason for this design decision? I understand
that this type of rounding can reduce the overall error in long
computation chains by reducing the amount of error that accumulates
from ignoring the fractional part of intermediate results, but why
specifically does VB.NET do this? It seems very unnatural IMO; I can't
think of any other programming language that does this. It would make
much more sense if VB.NET performed floating-point to integer
conversions by simply dropping everything after the decimal. It makes
even less sense to have to use the integer division ('\') operator when
you need to make it explicit that you -don't- want it to perform
rounding. On that note, I think they got the '/' and '\' syntax
backwards. IMHO...
The '/' operator (what is currently
floating-point-division-with-rounding-on-conversion-to-integer) should
behave similar to how the '/' operator behaves in most other
programming languages: the division should be performed in
floating-point; if the result is then converted to an integer, the
fractional part should simply be thrown out without any rounding.
The '\' operator (so-called "integer division") should do what '/'
currently does in VB.NET. That is, it should perform the division and
then round up if the fractional part is greater than or equal to 0.5.
Think about it. Most programming languages only have one division
operator, which is usually written with '/' by convention. And by
convention, '/' in most programming languages means what '\' (integer
division) means in VB.NET. VB.NET is completely upside-down. Sometimes
I get the feeling that Microsoft intentionally goes out of its way to
avoid following conventions or precedent, or anything that might be
considered 'normal.' ;-)
That's my rant. I'm interested to see what everyone else thinks of all
this, and if anyone indeed knows why Microsoft chose to do division
this way.
floating-point division ('/') is -rounded- on being converted to an
integer type, such as with statements like
Dim x As Integer = 2/3 'after assignment, x is 1, whereas a sane person
would say it should be 0
Does Microsoft have a reason for this design decision? I understand
that this type of rounding can reduce the overall error in long
computation chains by reducing the amount of error that accumulates
from ignoring the fractional part of intermediate results, but why
specifically does VB.NET do this? It seems very unnatural IMO; I can't
think of any other programming language that does this. It would make
much more sense if VB.NET performed floating-point to integer
conversions by simply dropping everything after the decimal. It makes
even less sense to have to use the integer division ('\') operator when
you need to make it explicit that you -don't- want it to perform
rounding. On that note, I think they got the '/' and '\' syntax
backwards. IMHO...
The '/' operator (what is currently
floating-point-division-with-rounding-on-conversion-to-integer) should
behave similar to how the '/' operator behaves in most other
programming languages: the division should be performed in
floating-point; if the result is then converted to an integer, the
fractional part should simply be thrown out without any rounding.
The '\' operator (so-called "integer division") should do what '/'
currently does in VB.NET. That is, it should perform the division and
then round up if the fractional part is greater than or equal to 0.5.
Think about it. Most programming languages only have one division
operator, which is usually written with '/' by convention. And by
convention, '/' in most programming languages means what '\' (integer
division) means in VB.NET. VB.NET is completely upside-down. Sometimes
I get the feeling that Microsoft intentionally goes out of its way to
avoid following conventions or precedent, or anything that might be
considered 'normal.' ;-)
That's my rant. I'm interested to see what everyone else thinks of all
this, and if anyone indeed knows why Microsoft chose to do division
this way.