scriptsharp icon indicating copy to clipboard operation
scriptsharp copied to clipboard

int division

Open jimmygilles opened this issue 13 years ago • 2 comments

Hello,

I saw a little problem with the following code :

Int32 a = 5;
Int32 b = 2;
Int32 c = a / b;

In an application, the value of c will be 2, which is correct.

This code is converted to javascript in:

var a = 5;
var b = 2;
var c = a / b;

In that situation, c will be 2.5 which is not the expected result. I saw that when we use an explicit cast, parseInt is added.

I am not sure if this is something easy to fix, but maybe when a division between 2 integers is detected, parseInt could be added automatically ?

Thank you !

jimmygilles avatar Aug 31 '11 21:08 jimmygilles

I am having a similar issue with casting between implicit casting with ints.

            int port = 10 + Math.Random() * 4000;

compiles into

            var port=10 +Math.random()*4000;

This

            int port = 10+ ((int) Math.Random()*4000);

compiles into

            var port=10 +Math.random()*4000;

Finally I had to this to parse the number back to an int properly.

            int port = 10 + Math.Truncate((Number)(Math.Random() * 4000));
            var port = 10 + parseInt((Math.random() * 4000));

Is the issue that the implicit casts are not available to you during parsing?

dested avatar Jul 17 '12 02:07 dested

Indeed, it would be great if Script# would handle integer division just a a C# user would expect. When both operands of the division are integer is should generate the following code: (a / b | 0) instead of the current: a / b

HazardX avatar Jan 06 '13 22:01 HazardX