Results 1 to 10 of 10

Thread: C++ function to Pascal problem...

  1. #1

    C++ function to Pascal problem...

    Hi,

    Im having a problem with a function converted for me by another member, it seems to create a value out of range.

    the delphi conversion is:
    [pascal]
    function carmack_func(const x: Single): Single;
    var
    halfx: Single;
    carmack: Integer;
    begin
    halfx := x*0.5;
    carmack := $5f3759df-(Trunc(x) Shr 1);
    result := carmack*(1.5-halfx*carmack*carmack);
    end;
    [/pascal]

    and the c++ version is:
    Code:
    inline float __fastcall carmack_func(float x)
    {
    	int carmack;
    	float isx, halfx;	//Inverse Squareroot of x
    	halfx = 0.5f*x;
    	carmack = *(int*)&x; 
    	carmack = 0x5f3759df - (carmack>>1); 
    	isx = *(float*)&carmack; 
    	isx = isx*(1.5f-halfx*isx*isx);  //Newton-Rhapson step, add more for accuracy
    	return isx;
    }
    and to be honest, im rather confused and dont understand this function ops:

    Any help would be great
    Many thanks
    Nic
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  2. #2

    C++ function to Pascal problem...

    Doh!!!

    Nevermind me.... it figures i would find the answer after a made a post

    [pascal]
    function carmack_func(x: Single): Single;
    var
    XHalf: Single;
    i: Integer Absolute x;
    X2: Single Absolute i;
    XB: Single;
    begin
    XB := x;
    XHalf := 0.5*x;
    i := $5f3759df-(i Shr 1);
    x := X2*(1.5-XHalf*X2*X2);
    Result := XB*x;
    end;
    [/pascal]

    Thanks
    Nic
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  3. #3

    C++ function to Pascal problem...

    Hello again,

    i have another c++ line that i dont really get...

    Code:
    	glVertex3fv&#40;!&#40;Cloud->Center + &#40;Cloud->vx + Cloud->vy&#41; * -Cloud->Radius&#41;&#41;;
    What is with the '!' im learning some c++ as i go along, but i have not found any documents that cover using it in this way, any ideas?

    many thanks
    Nic
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  4. #4

    C++ function to Pascal problem...

    Quote Originally Posted by M109uk
    Hello again,

    i have another c++ line that i dont really get...

    Code:
    	glVertex3fv&#40;!&#40;Cloud->Center + &#40;Cloud->vx + Cloud->vy&#41; * -Cloud->Radius&#41;&#41;;
    What is with the '!' im learning some c++ as i go along, but i have not found any documents that cover using it in this way, any ideas?

    many thanks
    Nic
    ! in c++ means same as NOT in pascal
    From brazil (:

    Pascal pownz!

  5. #5

    C++ function to Pascal problem...

    Quote Originally Posted by arthurprs
    ! in c++ means same as NOT in pascal
    That's right, so the line should be like this:
    Code:
    glVertex3fv&#40;not&#40;Cloud.Center + &#40;Cloud.vx + Cloud.vy&#41; * -Cloud.Radius&#41;&#41;;

  6. #6

    C++ function to Pascal problem...

    Quote Originally Posted by Brainer
    That's right, so the line should be like this:
    Code:
    glVertex3fv&#40;not&#40;Cloud.Center + &#40;Cloud.vx + Cloud.vy&#41; * -Cloud.Radius&#41;&#41;;
    Doesn't make much sense though. glVertex3fv takes 1 parameter that is pointer to a vector (that has x,y,z: single).

  7. #7

    C++ function to Pascal problem...

    Thanks for the replys,

    Im aware that it means not, however i have no idea how to use it in this circumstance, since not returns a bool and not a pointer, which is what the glvertex3fv wants.

    Would it be like the opposite of what im sending, ie (15, 15, 15) to (-15, -15, -15)?

    Many Thanks
    Nic
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  8. #8

    C++ function to Pascal problem...

    Well, C++ supports operator overloading, so you'll have to look at the proper definition/documentation, but it is highly possible that ! inverts vectors, ie. (15,15,15) -> (-15,-15,-15)
    blog: http://alexionne.blogspot.com/

  9. #9

    C++ function to Pascal problem...

    Make an inline function for the "not" for vectors, like "Inverse(aVector: TVector): TVector"

    FPC can overload operators but AFAIK "not" isn't among those which are allowed. (:=, <>, >, <, = are all overloadable)
    Feel the power of Open Source.
    <br />Feel the power of Free Pascal.

  10. #10

    C++ function to Pascal problem...

    [pascal]operator not(a: TVector)result: TVector;
    begin
    result.x := -a.x;
    result.y := -a.y;
    end;[/pascal]
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

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
  •