The question is answered, right answer was accepted
Does this Truncate formula seem efficient?
So I've been scripting a game that uses a Stat system. I've been using mainly ints and a few floats for my numbers. Recently, I had to add some more floats and change some ints to floats. After fixing the compile errors associated with that mess, I found that I want to Truncate some of the floats. NOT ROUND, Truncate to 0 decimals. After an hour or 2 sifting through the forums for a truncate float method, I determined that I couldn't find one for float, only doubles or by some casting to int method that just seemed like too much. I decided to use the Mathf.Round()
method to create stand-in Truncate formula.
I was wondering if this was efficient enough to not have to do any double conversion or int casting and just use this for truncating floats from now on. Mathf.Round(N - 0.5f)
I don't see any problem with it so far, But I haven't used any absurdly finite floats or delved into the arbitrary math expressions realm with it yet.
Answer by DawidNorasDev · Nov 06, 2018 at 08:54 AM
You mean Mathf.Floor(N);
?
I guess my lack of knowledge precedes me after looking that up.
Answer by michi_b · Nov 06, 2018 at 09:44 AM
First, you are not truncating, but flooring (rounding down), which is different for negative numbers. Also, the Mathf.Round(N - 0.5f) won't even floor correctly. Math.Round() and Mathf.Round() have the quirk that they round exact midpoints to the even number, as documented. That means, that Mathf.Round(N - 0.5f) will return 2.f for 2.f, but 0.f for 1.f.
For actual truncation, I guess that the cast to int and back to float would be most efficient, actually.
I understand the negatives and didn't think about that since I won't be using them on this project.
However, for my positive numbers, I've seen the results I've been looking for and they seem to contradict what you're saying... Unless i'm misunderstanding you.
Replacing N with $$anonymous$$yFloat for readablilty
From my tests:
public float $$anonymous$$yFloat;
$$anonymous$$yFloat = 1.93463f;
Debug.Log($$anonymous$$athf.Round($$anonymous$$yFloat - 0.5f)); //Returns 1
$$anonymous$$yFloat = 1.153453f;
Debug.Log($$anonymous$$athf.Round($$anonymous$$yFloat - 0.5f)); //Returns 1
$$anonymous$$yFloat = 0.92353f;
Debug.Log($$anonymous$$athf.Round($$anonymous$$yFloat - 0.5f)); //Returns 0
$$anonymous$$yFloat = 0.125353f;
Debug.Log($$anonymous$$athf.Round($$anonymous$$yFloat - 0.5f)); //Returns 0
These seem to produce that I'm looking for every time, with the new exception of negatives.
So, I think I'm misunderstanding your statement?
$$anonymous$$ath.Round() and $$anonymous$$athf.Round() have the quirk that they round exact midpoints to the even number, as documented. That means, that $$anonymous$$athf.Round(N - 0.5f) will return 2.f for 2.f, but 0.f for 1.f
**EDIT Also, I understand that it's not true truncating. I guess I should've specified pseudo truncating?
**EDIT 2 After looking up the method given in the first answer, I understand my lack of knowledge. I could just replace my formula with the method $$anonymous$$athf.Floor($$anonymous$$yFloat)
It's exactly the same thing and I did not know of it beforehand
Yep, $$anonymous$$athf.Floor is mostly the same thing, except for the exact midpoints. $$anonymous$$idpoint Rounding is what happens, when the parameter to $$anonymous$$athf.Round is exactly between two integers (1.5f, 2.5f etc.). With your formula, that happens when $$anonymous$$yFloat has no fractional part, because you subtract 0.5f, so only for 0.f, 1.f, 2.f etc. Each even number will stay the same, but each uneven number will return the next lower integral. For System.$$anonymous$$ath.Round you have two options for midpoint rounding. In UnityEngine.$$anonymous$$athf.Round it's always "to even".
Follow this Question
Related Questions
C# Unity dot syntax exercise correct solution? 1 Answer
Opening and closing a Gate on Input.GetButtonDown 0 Answers
OnCollisonEnter2D Not Firing after checking collider 1 Answer
How to alter Right/Top Value from RectTransform 0 Answers
FormatException: Input string was not in the correct format 0 Answers