Round

From ZDoom Wiki
Jump to: navigation, search

fixed Round (fixed value)

Usage

Rounds the specified value to the nearest whole number.

Return value

The value after rounding, as a fixed-point number.

Custom function

Note: The following is a non-native implementation of the function, for versions prior to 1ff4016.

This function implements rounding. When performing the bitshift operation on a fixed point number, what you are actually doing is simply cutting off the end of the number, the part that is less than 1. So 0.5 >> 16 will return 0. There may be cases where you want to round values of 0.5 or greater to the nearest whole number instead when converting it to an integer, for example placing an absolute location for a hudmessage.

If you need to round a number to the nearest whole but need the resulting number as a fixed point, you can just bitshift it again in the opposite direction using round(number) << 16.

// Returns integer value
function int round(int fixedNumber)
{
	return (fixedNumber + 0.5) >> 16;
}

Cut off decimal part version

// Returns fixed point value
function int floor(int fixedNumber)
{
	return fixedNumber & 0xFFFF0000;
}

Examples

Nuvolachalk.png Note: This article lists no examples. If you make use of this feature in your own project(s) or know of any basic examples that could be shared, please add them. This will make it easier to understand for future authors seeking assistance. Your contibutions are greatly appreciated.