Page 1 of 1

float to int

Posted: Thu Feb 22, 2024 9:38 am
by prime
Hi all,

I understand that unrealscript will coerse float values to int if you do something like :

Code: Select all

var int i;
var float f;

f = 5.7;
i = f;
However what arrived in i? Does it round up, round down, or just ignore the fractional part?
Are there any convenient functions that can force the issue?

Cheers.

Phill.

Re: float to int

Posted: Thu Feb 22, 2024 10:26 am
by Buggie
It truncate. For round, you can add 0.5 before cast to positive numbers and subtract 0.5 for negative.

Code: Select all

	fv = 5.7;
	return BadParameters("f =" @ fv @ "(int)f =" @ int(fv) @ "(int)-f =" @ int(-fv)
		@ "(int)(f + 0.5) =" @ int(fv + 0.5) @ "(int)(-f - 0.5) =" @ int(-fv - 0.5));

Code: Select all

---------------------------
Warning
---------------------------
f = 5.700000 (int)f = 5 (int)-f = -5 (int)(f + 0.5) = 6 (int)(-f - 0.5) = -6
---------------------------
ОК   
---------------------------

Re: float to int

Posted: Sun Feb 25, 2024 11:36 am
by Eternity
Also, keep in mind unreal script has float to int cast bugs...