Results 1 to 4 of 4

Thread: [solved] convert floating point to integer

  1. #1

    [solved] convert floating point to integer

    System: XP pro on virtualbox
    Compiler\IDE: fpc 2.2.4
    API: OpenGLes (mbx lite)

    I am trying to convert floating point values to integer. I need that because i need to support hardware that does not support floating point values.
    For now i use the following function:
    [code=pascal]function f2vt(value: single): integer;
    var
    temp: integer;
    begin
    temp := integer( single(value) * single(65535));
    writeln(integer(temp));
    result := integer(temp);
    end;[/code]

    But the results are all wrong for negative numbers.
    if i feed -0.4 i get: -2147457434 (i expected -26214)
    if i feed 0.4 i get: 26214 (correct)

    http://3das.noeska.com - create adventure games without programming

  2. #2

    Re: convert floating point to integer

    [code=pascal]
    function f2vt(value: single): integer;
    var
    temp: integer;
    begin
    temp := integer(trunc(single(value) * single(65535)));
    writeln(integer(temp));
    result := integer(temp);
    end;
    [/code]

    works here, though I don't know if it is a suitable solution for you
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  3. #3

    Re: convert floating point to integer

    Thank you. That seems to work for me too.
    http://3das.noeska.com - create adventure games without programming

  4. #4

    Re: [solved] convert floating point to integer

    You are welcome
    I had similar problems converting libnds for fpc. It was a real nightmare, because I had to check all those functions for type conversions, value by value, one by one, comparing the results from c and pascal code, and testing it on an emulator and on real hardware
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

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
  •