Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Fixed point math

  1. #1

    Fixed point math

    System: Windows XP sp2
    Compiler: arm-nds-fpc 2.1.4. i386-win32
    API: none

    I need to do some conversion from floating point values to 1.19.12 fixed point. The code I have found in C is:
    Code:
    #define floattof32&#40;n&#41;        &#40;&#40;int32&#41;&#40;&#40;n&#41; * &#40;1 << 12&#41;&#41;&#41;
    that I have tried to convert in this way:

    [pascal]function floattof32(n: cfloat): cint32; inline;
    begin
    floattof32 := (cint32(n) * (1 shl 12));
    end;[/pascal]

    but does not work as I expected. Can anyone help me here?
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  2. #2

    Fixed point math

    Heh, I had a whole reply typed up when I noticed something. Look at the C macro again for a second. The typecast is on the result of the shift and multiplication, and not the number before the multiplication. So your function should be:[pascal]function floattof32(n: cfloat): cint32; inline;
    begin
    floattof32 := cint32((n) * (1 shl 12));
    end;[/pascal]

  3. #3

    Fixed point math

    Uh-oh... You are right... ops:
    However it does not work too
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  4. #4

    Fixed point math

    Don't worry, I shot myself in the foot with a sizable if-then-else block the other day (just about driving myself nuts in the process). What exactly are you trying to do? I don't understand the multiplication in there. If you wanted to store the whole 32bit number as an integer you would call move() and shift the value over.

  5. #5

    Fixed point math

    I need to store the floating point value in a 32 bit integer, where the 1st bit is the sign, bits from 2 to 20 are holding the integer part and 21 to 32 the fraction part. It's for 3d on Nintendo DS, because this particular format number is used to handle matrix transformations via hardware.
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  6. #6

    Fixed point math

    Ouch. I really don't know how to help you then. Maybe someone else can who has a better knowledge of those things.

  7. #7

    Fixed point math

    Do you have an example of type of input and expected output?
    <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 =-

  8. #8

    Fixed point math

    Quote Originally Posted by savage
    Do you have an example of type of input and expected output?
    Thanks, mates. At this time I'm at work. As soon as I'll come back home I'll make it
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  9. #9

    Fixed point math

    btw, in Delphi 6 the previous code you posted does not compile as I get an invalid typecast error.

    The following code appears to convert between the floats and Ints ( to a few decimal places ), but I'm not sure if it is what you are looking for...

    [pascal]
    function FloatToInt32(n: single): integer;
    begin
    result := Round((n) * (1 shl 12));
    end;

    function Int32ToFloat(n: integer): single;
    begin
    result := ( (n) / (1 shl 12) );
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    ShowMessage( FloatToStr( Int32ToFloat( FloatToInt32( 1.1 ) ) ) );
    end;
    [/pascal]
    <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 =-

  10. #10

    Fixed point math

    I have tried every possible combination of trunc/round/shl/shr, but the obvious one:

    [pascal]function floattof32(n: cfloat): cint32; inline;
    begin
    floattof32 := trunc((n) * (1 shl 12));
    end;[/pascal]

    This one seems working as expected, though I have learned "never think that something works, even when it seems working". Thanks again, guys! And stay tuned, because I'm making some other strange type conversion routines :lol:
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

Page 1 of 2 12 LastLast

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
  •