Why is this code returning different values? ( C# and VB.NET ) -


vb.net code:

module module1  sub main()     dim x, y single     x = 0 + (512 / 2 - 407) / 256 * 192 * -1     y = 0 + (512 / 2 - 474) / 256 * 192     console.writeline(x.tostring + ": " + y.tostring)     console.readline() end sub  end module 

returns: 113,25: -163,5

c# code:

class program {     static void main(string[] args)     {         float x, y;         x = 0 + (512 / 2 - 407) / 256 * 192 * -1;         y = 0 + (512 / 2 - 474) / 256 * 192;         console.writeline(x + ": " + y);         console.readline();     } } 

returns 0: 0

i don't it, appreciate explanation.

c# / performs integer division, truncating fractional portion. vb.net implicitly casts double.

to perform floating point division, cast floating point type:

    static void main(string[] args)     {         float x, y;         x = 0 + (512 / 2 - 407) / (float)256 * 192 * -1;         y = 0 + (512 / 2 - 474) / (float)256 * 192;         console.writeline(x + ": " + y);         console.readline();     } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -