FMath::DivideAndRoundDown() rounds up (to zero) for negatives

Build Type: Binary

Build Version: 4.13.0 - 3106830

Detailed Description of the Issue:
The function FMath::DivideAndRoundDown ( UnrealMathUtility.h:287) Doesn’t actually round down when the result of the division is a negative number (it rounds to zero).

Example:
This snippet loops over the number -10 to 10 and uses FMath::DivideAndRoundDown to divide them by 5:

	for (int32 i = -10; i <= 10; i++)
	{
		UE_LOG(LogTemp, Warning, TEXT("FMath::DivideAndRoundDown(%d, 5) => %d"), i, FMath::DivideAndRoundDown(i, 5));
	}

This code prints the following to the log:

LogTemp:Warning: FMath::DivideAndRoundDown(-10, 5) => -2
LogTemp:Warning: FMath::DivideAndRoundDown(-9, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(-8, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(-7, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(-6, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(-5, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(-4, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(-3, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(-2, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(-1, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(0, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(1, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(2, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(3, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(4, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(5, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(6, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(7, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(8, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(9, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(10, 5) => 2

As you can see, if the function were to actually round down, the negative numbers should result in a combination of -2 and -1, but the actual result is symmetrical to the equivalent positive numbers.

The output should actually be this, if it were to round down:

LogTemp:Warning: FMath::DivideAndRoundDown(-10, 5) => -2
LogTemp:Warning: FMath::DivideAndRoundDown(-9, 5) => -2
LogTemp:Warning: FMath::DivideAndRoundDown(-8, 5) => -2
LogTemp:Warning: FMath::DivideAndRoundDown(-7, 5) => -2
LogTemp:Warning: FMath::DivideAndRoundDown(-6, 5) => -2
LogTemp:Warning: FMath::DivideAndRoundDown(-5, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(-4, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(-3, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(-2, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(-1, 5) => -1
LogTemp:Warning: FMath::DivideAndRoundDown(0, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(1, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(2, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(3, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(4, 5) => 0
LogTemp:Warning: FMath::DivideAndRoundDown(5, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(6, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(7, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(8, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(9, 5) => 1
LogTemp:Warning: FMath::DivideAndRoundDown(10, 5) => 2

Hey PJB3005-

The function appears to be working as intended. What’s happening is that because you are passing in int values, the result of the division is being truncated (the decimal is being dropped) before any rounding occurs. This leads to the results that you’re seeing since -9 / 5 = -1.8 (the .8 is being dropped and -1 rounded returns -1).

Cheers

Doug Wilson