PDA

View Full Version : Converting/casting cpp datatypes



Balaras
29-07-2005, 01:57 PM
Hi,

I'm converting some cpp code to work in my pascal project.
(BjA¬?rn BergstrA¬?m's recursive shadowcasting (http://www.roguelikedevelopment.org/php/category/showCategory.php?path=development/&category=LOS))

The following line that casts from integer to double to integer, where xCenter is an integer:
int xStart=(int)((double)xCenter + 0.5 - (startSlope * distance));

xStart := Integer( Double(xCenter) + 0.5 - (startSlope * distance) ) yields an invalid typecast error.

So I though about doing something like this:
xStart := Round( xCenter + 0.5 - (StartSlope * distance) );

Would this give the correct result ?

Thanks,
Balaras

technomage
29-07-2005, 02:07 PM
That should do the trick..but...if the origional code just returned the whole number without rounding (which I suspect it would) I would say use Trunc instead.

Dean

Balaras
29-07-2005, 02:14 PM
Thanks Dean,

To truncate a real would be the equivallent of floor() right ?

Balaras

technomage
29-07-2005, 02:15 PM
I think so.

Clootie
29-07-2005, 04:00 PM
IIRC it should be:
xStart := TRUNC( xCenter + 0.5 - (StartSlope * distance) );

Balaras
29-07-2005, 05:07 PM
Thanks both of you.