Page MenuHomeFeedback Tracker

Losing precision on float value
New, UrgentPublic

Description

I have found a bug where you are losing precision on float.

Details

Severity
Major
Resolution
Open
Reproducibility
Always
Operating System
Windows 10 x64
Category
General
Steps To Reproduce
modded class MissionGameplay extends MissionBase
{
    float gettest()
	{
		return 0.01;
	}

	float getprice()
	{
		float price = 0;
		int amount = 10;

		while ( amount > 0 )
		{
			price += gettest();
			amount--;
		}

		return price;
	}
    
    override void OnKeyPress(int key)
	{
        super.OnKeyPress(key);

		if ( key == KeyCode.KC_B )
		{
			float price = getprice();
			string txt = " price : " + price + " test 1 : " + (price - 0.1);
			GetGame().GetMission().OnEvent(ChatMessageEventTypeID, new ChatMessageEventParams(CCDirect, "", txt, ""));
		}
    }
}

That's a screen to see what it's doing:

Additional Information

The step to reproduce give you a code to put inside a mod or a modded class, it's will print you the price in chat when you press B, you see it's telling you 0.1, but in reality if you do price - 0.1 like I do, you see it's a little number like 3.27888e-08, but normally it's would be 0.

That's a really annoying bug for us in Expansion. I took a day to find where it's come from and to put a step to reproduce that's working in vanilla too.

Event Timeline

NiiRoZz created this task.Jun 28 2019, 11:22 PM
NiiRoZz edited Steps To Reproduce. (Show Details)
NiiRoZz edited Additional Information. (Show Details)
NiiRoZz edited Steps To Reproduce. (Show Details)Jun 28 2019, 11:26 PM
NiiRoZz edited Steps To Reproduce. (Show Details)
NiiRoZz edited Steps To Reproduce. (Show Details)
AussieCleetus added a subscriber: AussieCleetus.EditedJun 28 2019, 11:27 PM

I can verify this happening in several locations, floats losing their accuracy.

	void ExportScene()
	{
		SaveScene();
		string toCopy;
		string modelName;
		vector modelPosition;
		vector modelOrientation;
		vector modelSize;
		TStringArray strs = new TStringArray;
		string modelNameCleaned;
		float modelRelPosY, modelAdjustedX;
		ref array<ref ModelDataClass> m_ModelData = new array<ref ModelDataClass>;
		
		foreach (Object m_object : m_Objects)
		{
			modelName = m_object.ConfigGetString("model");
			modelName.Split("\\", strs);
			modelNameCleaned = strs[(strs.Count() - 1)];
			modelNameCleaned.Replace(".p3d", "");
			modelPosition = m_object.GetPosition();
			modelOrientation = m_object.GetOrientation();
			modelSize = GetObjectSize(m_object);
			GetGame().ObjectDelete(m_object);
			m_ModelData.Insert(new ModelDataClass(modelNameCleaned, modelPosition, modelOrientation, modelSize));
		}
		
		foreach (ModelDataClass m_model : m_ModelData)
		{
			modelRelPosY = m_model.m_Position[1] - GetGame().SurfaceY(m_model.m_Position[0], m_model.m_Position[2]) - (m_model.m_Size[1] / 2);
			modelAdjustedX = m_model.m_Position[0] + 200000.00001;
			toCopy = toCopy + "\"" + m_model.m_Name + "\";" + modelAdjustedX + ";" + m_model.m_Position[2] + ";" + m_model.m_Orientation[0] + ";" + m_model.m_Orientation[1] + ";" + m_model.m_Orientation[2] + "; 1.0;" + modelRelPosY + ";\n";
		}

		GetGame().CopyToClipboard( toCopy );

		Message( "Copied to clipboard" );
		m_Objects.Clear();
		LoadScene();
	}

this is a modified export function for Community-Online-Tools, the 200000.00001 was part of my tests to figure out why the floating point precision was being lost (thinking that it would solve it, but it doesn't)

NiiRoZz edited Additional Information. (Show Details)Jun 28 2019, 11:29 PM
mov3ax added a subscriber: mov3ax.Jun 28 2019, 11:29 PM