c++ -> Object Pascal
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);
}
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.
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.
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;
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;
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
http://druidgames.cjb.net/
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
http://druidgames.cjb.net/
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
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
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:
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
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 := -1else if D > 0 then Result := 1 //in C/C++, true = 1 andelse 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
Okay... *grinZ*
the
x := 1 shl VARIANCE_DEPTH
is cool.
The second one can be improved slightly:
This works coZ Delphi also assigns 1 for true and 0 for false.
Edited by - LoreKeeper on April 24, 2001 4:44:08 AM
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
Just came back to append what I said and mention SHL.. but looks like I was beaten to the punch... So what was it to do the loop? My Pascal skills are sooooo old and rusty
-Chris Bennett of Dwarfsoft - The future of RPGs Thanks to all the goblins in the GDCorner niche
-Chris Bennett of Dwarfsoft - The future of RPGs Thanks to all the goblins in the GDCorner niche
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement