Division in VB.NET -
what's difference between /
, \
division in vb.net?
my code gives different answers depending on use. i've seen both before, never knew difference.
there 2 ways divide numbers. fast way , slow way. lot of compilers try trick doing fast way. c# 1 of them, try this:
using system; class program { static void main(string[] args) { console.writeline(1 / 2); console.readline(); } }
output: 0
are happy outcome? technically correct, documented behavior when left side , right side of expression integers. fast integer division. idiv instruction on processor, instead of (infamous) fdiv instruction. entirely consistent way curly brace languages work. major source of "wtf happened" questions @ so. happy outcome have this:
console.writeline(1.0 / 2);
output: 0.5
the left side double, forcing floating point division. kind of result calculator shows. other ways invoke fdiv making right-side floating point number or explicitly casting 1 of operands (double).
vb.net doesn't work way, / operator always floating point division, irrespective of types. do want integer division. that's \
does.
Comments
Post a Comment