everything ok works… but sometimes I’m doing some tests but during the upgrade I get numbers like 1.0000002 it’s not possible to be more precise about the increase??? in this case I am giving some precision 0.3
That’s how floats work. They are reasonably precise, and pretty fast, but not perfect.
Using a double (precision) variable is a lot more precise, but takes up more memory and performance in return
Thank you for your answer it’s a very long article a bit heavy and tiring to fully understand… you want it to depend on the topic… you want me to go through the translator… anyway you can read it… I understood that Mathf.Approximately could solve my problem… but I don’t understand how I could use them in my case…
so with double solves… I’m not interested in performance here… I have to change a lot of code in this case… can a variable inherent to damage be double?? why then the variable “public static float bulletDmg;” will accept to be equal to the double??? because I already had to make many variables static to allow for changes…
Just accept the precision for what it is. Don’t try to use double variables, it’s just going to snowball and complicate everything, yet it’s just delaying the inevitable. You can’t describe a base-10 fraction in binary numbers without some discrepancies in the least-significant digits.
If you want to check if X is approximately equal to Y, use Mathf.Approximately(X, Y). It’s basically saying the same thing as Mathf.Abs( (X - Y) ) < someTinyAmountCalledEpsilon.
Experiment with this online toy to explore float precision: IEEE-754 Floating Point Converter – in particular, enter 0.3 in the “You Entered” box, from your example.
Given their example, it looks like an accounting error. Banks avoid all of this by using very specific rounding rules at every operation (see: COBOL, BCD, string-encoding, etc.). When you want to present things to the user, just round it off. Discrepancies in the 7th decimal are not going to be interesting to players.
as you can see in the example… pressing a button increases a player’s statistic by 0.3f… a variable then … will show the current statistic … and it’s bad to see that for example the data are 0.8000001432 I would like to show in that variable simply 0.8 without the remainder
If you want to round a number to 1 decimal place, just multiply it by 10, round it and divide by 10 again. Also when you actually convert a number to a string, the ToString method can take a format string where you can specify how many digits should be shown.
In this example, the Mathf.RoundToInt function takes a float value numberToRound, and rounds it to the nearest integer. The resulting value is stored in the roundedNumber variable, which is then printed to the console using Debug.Log.
In this example, the Mathf.Round function is used to round a float value numberToRound to two decimal places. The number is first multiplied by 100 to move the decimal point two places to the right, then rounded to the nearest integer using Mathf.Round, and then divided by 100.0 to move the decimal point back two places to the left. The resulting value is stored in the roundedNumber variable, which is then printed to the console using Debug.Log.
Note that in the second example, you can change the number of decimal places you want to round to by changing the factor you multiply by in the Mathf.Round function. For example, to round to three decimal places, you would multiply by 1000 instead of 100.
thank you for your answer… but so in my case how could I do???
why the variable “public static float actualDamagePl1;”
and in a file and the visualization takes place in another file… “StatsPlayer”
trying with
“damageTxt.text = Mathf.Round(StatsPlayer.actualDamagePl2 * 100) / 100.0f”
I point it all out as a mistake… “cannot implicitly convert type float to string”
ok i solved…
frist use in start metodh
StatsPlayer.actualDamagePl2 = Mathf.Round(StatsPlayer.actualDamagePl2 * 100) / 100.0f;
then where i need
damageTxt.text = StatsPlayer.actualDamagePl2.ToString();
tnk to all