Assigning Float Variables?

Post » Thu Jun 21, 2012 8:18 am

I figured out a bug in my script had to do with assigning values to float variables. When assigning values to floats through a mathematical equation, at least one of the numbers in the equation must be a float.

Using the following code, the first seven messages will display "0" or "0.0" while the last seven will display "0.5" :

Event OnInit()	Debug.Notification("1: " + 1 / 2)	Utility.Wait(2)	Float TempFloat = 1 / 2	Debug.Notification("2: " + TempFloat)	Utility.Wait(2)	Int i1 = 1	Int i2 = 2	Debug.Notification("3: " + i1 / i2)	Utility.Wait(2)	TempFloat = i1 / i2	Debug.Notification("4: " + TempFloat)	Utility.Wait(2)	TempFloat = i1 / i2 * 1.0	Debug.Notification("5: " + TempFloat)	Utility.Wait(2)	TempFloat = i1 / 2	Debug.Notification("6: " + TempFloat)	Utility.Wait(2)	TempFloat = 1 / i2	Debug.Notification("7: " + TempFloat)	Utility.Wait(2)	Debug.Notification("8: " + 1.0 / 2)	Utility.Wait(2)	Debug.Notification("9: " + 1 / 2.0)	Utility.Wait(2)	TempFloat = 1.0 / 2	Debug.Notification("10: " + TempFloat)	Utility.Wait(2)	TempFloat = 1 / 2.0	Debug.Notification("11: " + TempFloat)	Utility.Wait(2)	TempFloat = 1.0 / 2.0	Debug.Notification("12: " + TempFloat)	Utility.Wait(2)	TempFloat = i1 / 2.0	Debug.Notification("13: " + TempFloat)	Utility.Wait(2)	TempFloat = 1.0 / i2	Debug.Notification("14: " + TempFloat)EndEvent

I don't remember this being explained anywhere in the wiki. Is this a simple programming/scripting concept that I didn't know about?
User avatar
Claire Vaux
 
Posts: 3485
Joined: Sun Aug 06, 2006 6:56 am

Post » Thu Jun 21, 2012 7:57 am

I usually initialize my float variables to 0.0 before assigning values to them and I don't believe I have run into this problem. But it looks like a bug with whatever part of papyrus is responsible for automatically casting ints to floats. Either way it doesn't look to be too detrimental.

What happens if you manually cast i1 and i2 to floats?

TempFloat = (i1 as Float) / (i2 as Float)
User avatar
Franko AlVarado
 
Posts: 3473
Joined: Sun Nov 18, 2007 7:49 pm

Post » Wed Jun 20, 2012 10:28 pm

If I cast them as floats, then it works correctly.
User avatar
Franko AlVarado
 
Posts: 3473
Joined: Sun Nov 18, 2007 7:49 pm


Return to V - Skyrim