PDA

View Full Version : / works as div in Chrome



savage
21-10-2007, 11:42 AM
I recently tried to port some managed Direct X code from Delphi to Chrome.

After all the code compiled, when it ran it did not behave in the same was as the Delphi.NET version ( I'm talking about the PointSprites demo ).

After about 2-3 hours of racking my brain ( I know I'm a bit slow up top ) and stepping through nearly all the code, it turns out that in Chrome, the / operator works like div if 2 integers are used.

For example :

var
fRand1 : Single;
FRand : System.Random;
begin
FRand := new System.Random;

fRand1 := (FRand.Next(int32.MaxValue)/int32.MaxValue) * Math.PI * 2.00;
end;


fRand1 will always equal 0 because int32.MaxValue is always an integer so the expression

(FRand.Next(int32.MaxValue)/int32.MaxValue)

equates to *zero*.

The only way to ensure a floating point value is to do what you do in C# or C/C++ which is to typecast at least one of the integer values like this...

fRand1 := (Single( FRand.Next(int32.MaxValue) ) / int32.MaxValue) * Math.PI * 2.00;


I have suggested to RemObjects, that this is quite a fundamental Pascal change that will break a fair amount of existing Pascal code if people decide to port to Chrome, but the RO team believes that consistency of mathematical operations is more important.

I personally think that this makes Chrome a Next Gen C#, rather than a Next Gen Object Pascal, but after all it is his company and his compiler, so he can do what ever he likes.

This is quite significant for us game developers, as a lot of us reuse algorithms from other projects and game development is all about the maths, hence why this is posted here as a sticky thread.

So please be aware of this fact when porting your code to Chrome.
It will save you some head scratching if you come from an Object Pascal background.

JernejL
21-10-2007, 11:57 AM
Agreed, unless there is a compiler directive to control this behavour, it shouldn't be called pascal at all.

JSoftware
21-10-2007, 12:05 PM
Can't you just overload the / operator for two integers to result in a real value?

savage
21-10-2007, 12:11 PM
Can't you just overload the / operator for two integers to result in a real value?

Of course you could, but people usually overload the / operator in the context of objects within their project, rather than the actual mathematical operation itself to bahave differently.

By the way, I am not saying there are not other work-arounds, I am just highlighting what I have found so that others don't make the same mistake I made and don't waste their time tracking down this sort of thing.

dmantione
21-10-2007, 01:08 PM
I have suggested to RemObjects, that this is quite a fundamental Pascal change that will break a fair amount of existing Pascal code if people decide to port to Chrome, but the RO team believes that consistency of mathematical operations is more important.


The reason why Pascal has two div operators is that Pascal assigns no type to constants. I.e. "2" can be both an integer and a real constant, 'a' can be both a char constant, a character array constant, a shortstring constant or an ansistring constant (and more!).


writeln(10/3);
writeln(10 div 3);


are defined to give different results. In for example, C, two div operators are not necessary, because constants do have a type:



printf("%d",10/3);
printf("%f",10.0/3.0);


The difference is here signalled by using ".0" to make the compiler clear we are dealing with a real constant. Constants in C also can have postfixes, for example 0L means the constant is of type "long".



I personally think that this makes Chrome a Next Gen C#, rather than a Next Gen Object Pascal, but after all it is his company and his compiler, so he can do what ever he likes.


Yes, Pascal is not just its syntax, but also its semantics. At least I don't use Pascal to be able to write "begin" and "end".