Results 1 to 5 of 5

Thread: / works as div in Chrome

  1. #1

    / works as div in Chrome

    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 :
    [pascal]
    var
    fRand1 : Single;
    FRand : System.Random;
    begin
    FRand := new System.Random;

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

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

    [pascal](FRand.Next(int32.MaxValue)/int32.MaxValue)[/pascal]

    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...

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

    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.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  2. #2

    / works as div in Chrome

    Agreed, unless there is a compiler directive to control this behavour, it shouldn't be called pascal at all.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  3. #3

    / works as div in Chrome

    Can't you just overload the / operator for two integers to result in a real value?
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  4. #4

    / works as div in Chrome

    Quote Originally Posted by JSoftware
    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.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  5. #5

    Re: / works as div in Chrome

    Quote Originally Posted by savage
    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!).

    [pascal]
    writeln(10/3);
    writeln(10 div 3);
    [/pascal]

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

    Code:
      printf&#40;"%d",10/3&#41;;
      printf&#40;"%f",10.0/3.0&#41;;
    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".

    Quote Originally Posted by savage
    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".

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •