scriptsharp
scriptsharp copied to clipboard
int division
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 !
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?
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