float repair??

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

if (selectPlayer == 0)
        {
            if (PickUpCollected.goldDiamond >= costDamagePl1)
            {
                {
                    StatsPlayer.actualDamagePl1 += 0.3f;
                    PickUpCollected.goldDiamond -= costDamagePl1;
                    costDamagePl1 = costDamagePl1 + 3;
                    PlayerPrefs.SetInt("GoldDiamond", PickUpCollected.goldDiamond);
                    PlayerPrefs.SetInt("CostDamagePl1", costDamagePl1);
                    PlayerPrefs.SetFloat("ActualDamagePl1", StatsPlayer.actualDamagePl1);
                }
            }
        }

as you can see I ask to increase by 0.3f but the last strange number that came out is 6.099998 Why?? can it be solved??

https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

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…

You can cast to a float if it’s within the float range

Would you be so kind as to give me an example?

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.

Im with @halley1 here. Why do you have a problem with this precision problem?
What is your actual usecase, where this is problematic?

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

In your last post you quoted halley:

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.

and how do i do this??? programmatically how should I write??

could you post me an example or a link of how to round???

If by “want to show” you mean “display the value on the screen” then you can round a float to a specific number of decimal places using ToString():

Here are some examples:

Round to the nearest integer:

float numberToRound = 4.6f;
int roundedNumber = Mathf.RoundToInt(numberToRound);
Debug.Log(roundedNumber); // Output: 5

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.

  1. Round to a specified number of decimal places:
float numberToRound = 4.6789f;
float roundedNumber = Mathf.Round(numberToRound * 100) / 100.0f;
Debug.Log(roundedNumber); // Output: 4.68

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”

You just need to convert it to a string with .ToString() that every object has.

but where???
damageTxt.text = Mathf.Round(StatsPlayer.actualDamagePl2.ToString() * 100) / 100.0f; error
damageTxt.text = Mathf.Round(StatsPlayer.actualDamagePl2 * 100) .ToString() / 100.0f; error

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