Archived

This topic is now archived and is closed to further replies.

Pexi

c++ -> Object Pascal

Recommended Posts

I''m having problems with two pices of c++ code which I understand but don''t know how to translate them to Delphi. In the second pice I have problems spacially with the last line. 1. What does that do? (1< 0); }

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m not sure what number 1 is about (that is to say... I don''t know what the question is in no.1)

But 2:

return (d<0)?(-1)d>0);

means (to my knowledge)
if (d<0) return (-1); else return (d>0);

not sure what happens if d=0.

And I''m not sure... but it means something like that. Anybody out there, please verify.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The last line is just an if statement (d < 0) ? (-1) : (d > 0);
if d is less than 0 then return -1 otherwise return a boolean value of whether d is greater than 0 I think it could be something like this in Pascal
If (d<0) Then
Begin
d := -1;
else
d := (d > 0);
End;

Share this post


Link to post
Share on other sites
His first one got killed by HTML, here it is again: (1<<VARIANCE_DEPTH)

That means "1 shifted left by the value of the macro (I assume it''s a macro/constant/whatever) VARIENCE_DEPTH", and is probably done at compile time. I don''t know Pascal, so I can''t help you out.

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/

Share this post


Link to post
Share on other sites
For the first one. The difficult way of doing it is (forgive me, but my pascal is old and my brain is soggy).

Assume the C code reads like

X = (1<<VARIANCE_DEPTH);

INTEGER i,x;
...
i:=0;
x:=1;
REPEAT / LOOP (Gee... I haven''t done this for a while )
x := x*2;
i := i+1;
UNTIL i >= VARIANCE_DEPTH


Ugly eh? But a bit shift is a multiple of 2. It all makes sense in the end

-Chris Bennett of Dwarfsoft - The future of RPGs
Thanks to all the goblins over in our little Game Design Corner niche

Share this post


Link to post
Share on other sites
or you could just use the 'shl' and 'shr' operators for bitshifting

X := (1 shl VARIANCE_DEPTH);

return (d < 0) ? (-1) : (d > 0); is just an if-statement like previous posts mention, here's the object pascal code:

        
if D < 0 then
Result := -1
else if D > 0 then
Result := 1 //in C/C++, true = 1 and

else
Result := 0; //false = 0




BTW: as far as i know, there's no such thing as inlining in object pascal, if there is i'd like to here about it...

Edited by - kvh on April 23, 2001 2:01:41 PM

Share this post


Link to post
Share on other sites
Okay... *grinZ*

the

x := 1 shl VARIANCE_DEPTH

is cool.

The second one can be improved slightly:

  
if d<1 then
Result := -1
else
Result := d>0;


This works coZ Delphi also assigns 1 for true and 0 for false.


Edited by - LoreKeeper on April 24, 2001 4:44:08 AM

Share this post


Link to post
Share on other sites
quote:
Original post by dwarfsoft

For the first one. The difficult way of doing it is (forgive me, but my pascal is old and my brain is soggy).

Assume the C code reads like

X = (1<
INTEGER i,x;
...
i:=0;
x:=1;
REPEAT / LOOP (Gee... I haven''t done this for a while )
x := x*2;
i := i+1;
UNTIL i >= VARIANCE_DEPTH


The loop would have been this:

var
i, x: Integer;
begin
i := 0;
x := 1;
while (i < VARIANCE_DEPTH) do
begin
x := x * 2;
i := i + 1;
end;
...
end;

Using a do..while loop would always execute the loop once, resulting in a minimum shift of one, ie. a minimum x value of 2. But what if the shift was to be zero, ie. x = 1? That is why I chose a while loop.
quote:

Ugly eh? But a bit shift is a multiple of 2. It all makes sense in the end




Ugly, yes.

Steve ''Sly'' Williams  Code Monkey  Krome Studios

Share this post


Link to post
Share on other sites
Cheers... Thanks for that.. I haven''t coded Pascal for almost 2 Years! Sheesh... Anyway, that has refreshed my memories of the good ol'' days of "Legend Of the Red Dragon" (LORD - by RTSoft) and all the Turbo Pascal IGMs *sigh*

-Chris Bennett of Dwarfsoft - The future of RPGs Thanks to all the goblins in the GDCorner niche

Share this post


Link to post
Share on other sites