Menu

Fixed point reals

Cepreu
2010-11-09
2013-04-25
  • Cepreu

    Cepreu - 2010-11-09

    The following small program shows some problems with Fixed point reals:

    program New;
    var R:real; I: integer;
    begin
      Showform;
      R:=2.0/3;
      I:=FormAddString ('R: '+R+chr(10));
      R:=1.239888;
      I:=FormAddString ('R: '+R+chr(10));
      I:=FormAddString ('1.99: '+StringToReal('1.99', 10)+chr(10));
      I:=FormAddString ('1.2399: '+StringToReal('1.2399', 10)+chr(10));
      I:=FormAddString ('1.239888: '+StringToReal('1.239888', 10)+chr(10));
      delay(-1);
    end.

    If compiled with floating point option it works OK.

    In case of Fixed point reals:
    R: 0.66
    65 (instead of R: 0.6667)
    R: 1.23
    97  (instead of R: 1.2399)
    1.239888: 1.23
    97 (instead of 1.2399)

     
  • Javier Santo Domingo

    It's a very long dated hardware limitation with which MP as any other decent compiler has to deal with, so MP it's not very special on this. You can find some basic information about this subject in the documentation under "The MIDletPascal Dialect \ Data types \ Real type". Also some more detailed information in this two threads http://sourceforge.net/projects/midletpascal/forums/forum/1013750/topic/3651962?message=8249940 and http://sourceforge.net/projects/midletpascal/forums/forum/1013750/topic/3799651?message=8577555 where I wrote also about this. But may be the more realizing stuff is just reading the implementation of the compiler (specially in stdpas.c) relying in the stubs F.java and Real.java (you can find all of them in the SVN repository).
    Also, and besides it's an old topic you can find already many articles, posts, and stuff about this on a Google search, try for example "floating point J2ME".

    In any case, I will try to extend that old documentation if free time is on my side.

    Thanks for your interest!

     
  • Cepreu

    Cepreu - 2010-11-10

    Thank you for your answer!

    May be the problem is related with StringToReal function. The folowing program fragment compiled with fixed reals doesn't work (the program hangs up), while compiled with floating point real is OK (Accs.Saldo is a real);

      FSaldo := FormAddTextField('Saldo: ', ''+(Accs.Saldo), 20, TF_ANY);
      …
      WaitForm;    // waiting user to press OK
      Accs.Saldo:=StringToReal(FormGetText(FSaldo), 10);

     
  • Javier Santo Domingo

    Just in case, are you checking that the text that is being outputted by FormGetText() does not contains illegal characters for base 10? I see you are using TF_ANY, and that's ok since you need the user enter the "." sign, not only numbers, but when using TF_ANY the user can input anything else too, and that will hang StringToReal() for sure.

    Please try the following tests (as you can see I tryied your code too, everything works as expected here) and let me know the outcome, thanks.

    program testingReal;
      procedure ClearScreen;
      begin
        SetColor(255, 255, 255);
        FillRect(0, 0, GetWidth, GetHeight);
        SetColor(0, 0, 0);
      end;
    var r: real; s: string; FSaldo: integer; okCommand: command; a: record rr: real; end;
    begin
      // test: record-field := StringToReal(form-field, base 10)
      okCommand := createcommand('ok',CM_OK,1);
      showForm;
      addCommand(okCommand);
      ņœaldo := FormAddTextField('Saldo: ', ''+a.rr, 20, TF_ANY);
      while (getClickedCommand<>okCommand) do
        delay(100);
      a.rr := StringToReal(FormGetText(FSaldo), 10);
      showCanvas;
      DrawText(''+a.rr,0,0);
      Repaint;
      Delay(2000);
      ClearScreen;
      // test: real-variable := StringToReal(negative-literal-string, base 10)
      r := StringToReal('-9.8765', 10);
      DrawText(''+r,0,0);
      Repaint;
      Delay(2000);
      ClearScreen;
      //  test: real-variable := StringToReal(string-variable, base 16)
      s := 'ABCD.EF';
      r := StringToReal(s, 16);
      DrawText(''+r,0,0);
      Repaint;
      Delay(2000);
    end.
    
     
  • Cepreu

    Cepreu - 2010-11-11

    Thank you for your answer!

    Your program works here also. It is dificult to say what is the reason for program hanging - definetly FormGetText() does not contains illegal characters (there are no users - I'm just testing the program). If I use my own StrToReal procedure - the program works OK. If I put in StrToReal just to return the value ''+R - the program hangs again. If I compile hanging code with floating point reals - it works.

    By the way you can see the problem with changed by editor two letters in your code:

    ... ņœaldo := FormAddTextField('Saldo: ', ...
    
     
  • Cepreu

    Cepreu - 2010-11-11

    Sorry: my own StrToReal function

     
  • Javier Santo Domingo

    > By the way you can see the problem with changed by editor two letters in your code: … ņœaldo := FormAddTextField('Saldo: ', …

    Yeah, I saw it just after posting that and also tryed to reproduce with no success as you said before. A good hint on this is again pointing to the unicode and clipboard problems of SynEdit: I was working with a test program that was encoded in unicode since I was investigating your report of the two characters you made in the other post, and after that I based my test program of the Real numbers on it. Saw no problems until the last build in fact, but seems that when I made the Select All / Copy it just overwrote those chars. Of course then I tryed the same several times with no success… it was another bug in SynEdit, and as I said in the other thread I am somewhat tired of loosing my time fixing it. I said to me that it was enough of it, so before switching to Scintilla, decided to give a last chance to SynEdit since the official repository seems to be more active than the last years this days. The last revision of the SynEdit's SVN has a lot of bugs fixed like my patched version of the UniSynEdit that I was using (that was the recommended thing to do last year), so now I'm using it porting only one of my patches, the one that enables the column selection. In my tests everything is working ok, that bug does not appear, and unless I can reproduce it seems the next version will ship with this upgraded version of SynEdit. Of course that for the future MP 3.x releases I'm not that sure heh, if I have time I will give a try to Scintilla.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.