- Home /
Problem With float
Hi all.. so in the script im using here.. im getting the function "SpawnFaster" to decrease the value of spawnWait by .1 but if spawn wait = .1 then it will not decrease it.. so this was all going fine and i have a debug.log that tells me the value of it.. and at one point i got this very strange result :P and im not sure why.. It might have to do with the fact that i had to change .1 to .1f? im not sure :P Please help!!
also the value of spawnWait starts at 5
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour
{
public GameObject enemy;
public Vector3 spawnValues;
public int enemyCount;
public float spawnWait;
public float startWait;
public float ySpawnValue;
void Start ()
{
StartCoroutine (SpawnWaves ());
spawnWait = 5;
}
public void SpawnFaster ()
{
if (spawnWait > .1)
{
spawnWait -= .1f;
}
Debug.Log (spawnWait);
}
Answer by GiyomuGames · Aug 19, 2015 at 02:33 AM
It is actually normal. See "What Every Computer Scientist Should Know About Floating-Point Arithmetic" : http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
I guess this doesn't cause any problem does it?
If it's a problem then you can use int ins$$anonymous$$d. Start from 50 and remove 1 by 1.
On a related note, floating point imprecision is the key reason Unity provides the Appoximately operator.
When you need to compare whether two values are the same, floating point numbers have to be "close enough" ins$$anonymous$$d of identical to be reliable. If you just need to compare greater/less than, however, < and > are generally going to be reliable enough, since you don't need to worry *quite* as much.
@Rosscoe3:
"It does" isn't a good explanation why it's a problem... What exactly doesn't work the way you want / expected it to behave? If you don't put some more efford into presenting your actual problem we'll just close this question since it doesn't contain any concrete problem.
Answer by Rosscoe3 · Aug 20, 2015 at 01:30 AM
yes i understand.. sorry about that :( @Bunny83 i was currently trying to figure it out and did not give a good answer.. but i did figure it out.. i converted the variable spawnWait into a decimal.. and when needed turned it into a float.. i didn't post the whole code earlier.. but here it is:
using UnityEngine; using System.Collections;
public class Spawn : MonoBehaviour { public GameObject enemy; public Vector3 spawnValues; public int enemyCount; public decimal spawnWait; public float startWait; public float ySpawnValue;
void Start ()
{
StartCoroutine (SpawnWaves ());
//starts the funcion Spawnwaves
spawnWait = 5;
}
public void SpawnFaster ()
{
if (spawnWait > .1M)
//if the spawn wait is greater than the selected number.. it will not be triggered anymore
{
spawnWait = spawnWait - 0.1M;
//spawnWait is decreased by the selected number
}
//Debug.Log (spawnWait);
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for(int i = 0; i < enemyCount; i++)
//since i = 0 and enemyCount will always be greater than it.. if i becomes greater than enemyCount.. the loop stops
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));
//grabs a random range on the axis' of z and x
Quaternion spawnRotation = Quaternion.identity;
//not sure what the hell this does.. but code!!!
Instantiate (enemy, spawnPosition + transform.TransformPoint(0, 0, 0), spawnRotation);
//brings the enmy in at the spawnposition
yield return new WaitForSeconds ((float)spawnWait);
}
}
}
}