PDA

View Full Version : C++ function to Pascal problem...



M109uk
16-11-2007, 10:41 PM
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:

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;


and the c++ version is:


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 :oops:

Any help would be great
Many thanks
Nic

M109uk
16-11-2007, 10:54 PM
Doh!!!

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


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;


Thanks
Nic

M109uk
17-11-2007, 10:16 AM
Hello again,

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



glVertex3fv(!(Cloud->Center + (Cloud->vx + Cloud->vy) * -Cloud->Radius));


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

arthurprs
17-11-2007, 05:13 PM
Hello again,

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



glVertex3fv(!(Cloud->Center + (Cloud->vx + Cloud->vy) * -Cloud->Radius));


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 :)

Brainer
17-11-2007, 05:25 PM
! in c++ means same as NOT in pascal :)


That's right, so the line should be like this:

glVertex3fv(not(Cloud.Center + (Cloud.vx + Cloud.vy) * -Cloud.Radius));

User137
17-11-2007, 05:41 PM
That's right, so the line should be like this:

glVertex3fv(not(Cloud.Center + (Cloud.vx + Cloud.vy) * -Cloud.Radius));
Doesn't make much sense though. glVertex3fv takes 1 parameter that is pointer to a vector (that has x,y,z: single).

M109uk
17-11-2007, 06:16 PM
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

alexione
20-11-2007, 10:05 AM
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)

Almindor
22-11-2007, 08:28 PM
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)

JSoftware
23-11-2007, 12:00 AM
operator not(a: TVector)result: TVector;
begin
result.x := -a.x;
result.y := -a.y;
end; 8)