PDA

View Full Version : Truncate to integer?



Joe123
01-03-2008, 10:55 PM
Seeing as integers in ZScript aren't true integers, I can't see an obvious way of truncating values to integers.

I saw in ZScript.txt that that Pow(int base, int exp); function truncates to an integer, so what I'd been doing was Pow(value to be truncated, 1);, which works fine, I was just wondering if there was a less round-about way of doing it?

Ooo, 1100 posts.

pkmnfrk
01-03-2008, 11:22 PM
If you want to truncate, use Pow. If you want to round, add .5 and use Pow. Although, you could wrap it in a global function:



int Int(int v) { //too many ints O_o
return Pow(v, 1);
}

int Round(int v) {
return Pow(v+.5,1);
}


Actually, Round is bugged with negative integers. I'll leave it as an exercise to the reader to improve it >_>''

Joe123
01-06-2008, 09:17 AM
Ah yeah, thanks.

ScaryBinary
01-06-2008, 09:40 AM
...do the Ceiling and Floor functions in std.zh do what you're trying to do?

The following snippet of code...

Trace(1.445);
Trace(Ceiling(1.4455));
Trace(Floor(1.4455));


...produced the following in allegro.log...

1.4450
2.0000
1.0000

Or am I misunderstanding your intent?

PS: Those functions in std.zh are easy to overlook. I didn't even know they were there until just the other day...:D

pkmnfrk
01-06-2008, 10:20 AM
<_< Do they look very similar to the functions I, er, hand crafted?

*looks*

Why, yes they do. Except, they use shifting to do the same thing... x << 0 Hmm, I wonder which is faster? Probably the shift.

Joe123
01-06-2008, 10:35 AM
:eek:

I've used those before and everything.
Oops.

Thanks.