Dont understand how meth works

how do I transform this equasion into somthing the script can rid because I can seem to be able to do it

9748165--1394953--upload_2024-4-3_22-54-6.png
this is what I have come up with and its not working:

        int stat = Mathf.RoundToInt((Mathf.Pow(player.Str, (5 / 4)) * (Mathf.Pow(player.Dex, (2 / 3)) + 1)) / 10);

for Str = 16 and Dex = 12 the result is supposed to be 20 (19.smth but I round it) and it comes out 3 (3.2 but again I round it)

Division of integers rounds towards zero. You have to use a float for one of the parameters in order for the division to work as you intend (compiler treats it as a float operation instead of an integer operation).

1 Like

What do you mean by Division of integers? I know what ints are obviously

1/2 divides two integers. The result is integer 0 because the language has integer math operations result in integers.

This isn’t anything specific to Unity, by the way, it’s C# fundamentals.

1 Like

ok tnx

ok tnx
btw I know this is not unity-specific I have just gotten accustomed to the Unity forums already
so how do I turn one of the to float? just add .0 or smth like that?

An integer division happens when you divide an integer by another integer. If that happens the result will be an integer again. So you can not get any fractional numbers as result. So something like 5/4 is an integer division and will result in the number 1 while the division 2/3 will result in 0. You need to use float literal values instead. As soon as one of the operands is a float, it would be a float division and the result would be a float. So do

 int stat = Mathf.RoundToInt((Mathf.Pow(player.Str, (5f / 4f)) * (Mathf.Pow(player.Dex, (2f / 3f)) + 1f)) / 10f);

Not all f are necessary here, but when you work with floats, it’s best to use the proper literal value.

2 Likes

oh ok thank you

It needs to also have an f, so for instance 1.0f or just 1f, so it’s treated as a float instead of a double. Mathf.Pow expects floats. Integer types are automatically converted to float for the call, double is not.

2 Likes

It works now thank you all for helping me

A useful tip: make a general function for integer ratios that should evaluate as floating point. I do that all the time because you can’t make an integer division error by accident.

static public float ratio(int a, int b) => a / (float)b;

(As soon as one operand is explicitly cast to float, the other is converted automatically and the operation is inferred as floating point division. It also looks nicer in the user code, you don’t need to cast there, and you don’t need f suffix etc.)

Edit:
Obviously no need to do this when you’re working with literal values. This is more like a general approach for variables.

Edit2:
Also I’m astonished that no one made a joke out of your title typo :slight_smile:
So, here’s another answer to your question :smile:

Edit3:
This is how I’d write your expression

var strTerm = pow(str, 5f/4f); // you could just write 1.25f but these divisions are
var dexTerm = 1f + pow(dex, 2f/3f); // converted to value constants in compile time anyway
int stat = roundToInt(strTerm * dexTerm / 10f);
8 Likes

Lol I thought to my self how math is supposed to be typed but I just went with what came out first

1 Like

Yesterday I was going to write a joke about it, but felt lazy :smile:

1 Like

Not even one Breaking Bad joke?

5 Likes

what is var, I see it constantly being used but I am not really sure what it is

https://stackoverflow.com/a/4307487

In this context, it’s the same as float. You can find much more about it here.

ok tnx