2 int32s divided to make a float

Does anyone know if this or where this is at in the engine? All i find is divide 2 int32s to make an int32

I need 2 int32s divided to make a float. Any ideas, besides just writing it myself.

cast both ints into floats and divide them then with the float division.

This is the default in software development.

example:

int32 i1 = 5;
int32 i2 = 2;
float f1 = i1;
float f2 = i2;
float result = f1 / f2;

result will be 2.5f

Thanks, that is exactly what i did last night.

thumsup :slight_smile:

Here is what i wrote. use away or edit if needed. This will take int 2 int32s and spit out percentage text and a float. Also is blue print friendly :slight_smile:

H FILE

UFUNCTION(BlueprintCallable, Category = Roulette)
float Divide_TwoIntsToFloat_PlusPrecentOutText(int32 A, int32 B, FText& ThePercentage);

CPP FILE

//blueprint ran
float ASM_RouletteStats::Divide_TwoIntsToFloat_PlusPrecentOutText(int32 A, int32 B, FText& ThePercentage)
{
    float DivideBy = A; //MAKE THE A INT32 A FLOAT
    float TotalAmount = B; //MAKE THE B INT32 A FLOAT
    float YourPrecentage = DivideBy/TotalAmount;  // DIVIDE THE 2 TO GET YOUR PERCENTAGE
    YourPrecentage = UKismetMathLibrary::Abs(YourPrecentage);//GET RID OF THE FRACTIONAL SECTION
    ThePercentage = FText::Format(LOCTEXT("PerFormat", "Percentage is {0}"), FText::AsPercent(YourPrecentage));//SET FLOAT IN HERE IT WILL AUTOMATICALLY BE TIMES BY 100 AND SPIT OUT RIGHT PERCENTAGE VALUE
    return YourPrecentage;// RETURN THE FLOAT NOT TIMES BY 100, TO USE ELSEWHERE IF NEEDED.
}

Without wanting to be harsh, this is a little bit over the top :wink:
It’ll work but you can use basic functionality of c++ instead of using UKismetMathLibrary functions (which are basically blueprint functions).

Your calls are more complex , even so it will not make a huge difference (I suppose).

Simple operators like * (multiply) / (divide) % (modulo) + (addition) - (subtraction) are much more basic.

Furthermore conversion from int32 to float is also simply defined in c++ with the =-operator.

So…

int32 MyInt = 1;
float MyFloat = MyInt;

… is the fastest (performance wise) way to get MyFloat to be 1.0f

in other words, your meaning like this.

float DivideBy = A;//MAKE THE A INT32 A FLOAT
float TotalAmount = B;//MAKE THE B INT32 A FLOAT
float YourPrecentage = DivideBy / TotalAmount;// DIVIDE THE 2 TO GET YOUR PERCENTAGE

Yes. :slight_smile:

Like in my example above.

int32 i1 = 5;
int32 i2 = 2;
float f1 = i1;
float f2 = i2;
float result = f1 / f2;

I tried this, it did not like this. So, I figured just using the = would not work.
float testing = float(A/B); // A and B are int32s

I figured you had to convert the int32 to a float. Did not realize you could just = the int32 to be a float. Nice. Thanks again.

float(A/B) works wrong because A/B is calculated first.

Like in math things in brackets () are calculated first.

So let’s say you write …

float Result = float( 10 / 4 );

… than the system will calculated 10 / 4 first which should be 2 (as integer) and THAN he will create a float out of it and write it into the float Result which would be 2.0f . Which would not be what you expected. :slight_smile:

To get the correct result you could write…

float Result = float( 10 ) / float ( 4 );

… the system would than first convert the ints into float, which would be like…

float Result = 10.0f / 4.0f;

… and this would be Result = 2.5f … which would be the correct result. :slight_smile:

1 Like

Right on, I did not look at it like that. Sweet, thanks a bunch for your answers.