Float to Short conversion

Post » Thu Apr 08, 2010 1:55 am

I want to convert a float variable to a short, either rounding or truncating, it doesn't matter to me. Can I just set the short to the value of the float? Is there a trick involved?
User avatar
Mistress trades Melissa
 
Posts: 3464
Joined: Mon Jun 19, 2006 9:28 pm

Post » Thu Apr 08, 2010 4:36 am

If you assign a floating point value to an integer, it'll be truncated automatically. If you want to round it to the nearest whole value, the easiest method is just to add 0.5 then let it be truncated.

Cipscis
User avatar
Rachael
 
Posts: 3412
Joined: Sat Feb 17, 2007 2:10 pm

Post » Thu Apr 08, 2010 3:17 am

In practice, I have to work with float-short differences like this. This is where I need to take a value that comes in as a non-float (weaphealth) and make it be a float (weaphealthfloat) and use it in a float-requiring function.

short WeapHealthfloat WeapHealthFloat		if getequipped PhalanxButchReplaceSledges			set WeapHealth to getweaponhealthperc			set WeapHealthFloat to WeapHealth			set WeapHealthFloat to WeapHealthFloat * .01			additemhealthpercent WeapPhalanxButchSuperSledge 1 WeapHealthFloat


I'm aware that this is the other direction. But it makes sense to post it here.
User avatar
ILy- Forver
 
Posts: 3459
Joined: Sun Feb 04, 2007 3:18 am

Post » Thu Apr 08, 2010 10:01 am

Are you using your integer variable somewhere else? If not, then you can just use your floating point variable on its own and it'll work just fine:
set WeapHealthFloat to GetWeaponHealthPerc

In fact, you could shorten that code considerably if this is the case:
set WeapHealthFloat to GetWeaponHealthPerc * 0.01AddItemHealthpercent WeapPhalanxButchSuperSledge 1 WeapHealthFloat

Cipscis
User avatar
MARLON JOHNSON
 
Posts: 3377
Joined: Sun May 20, 2007 7:12 pm

Post » Thu Apr 08, 2010 11:45 am

Are you using your integer variable somewhere else? If not, then you can just use your floating point variable on its own and it'll work just fine:
set WeapHealthFloat to GetWeaponHealthPerc

In fact, you could shorten that code considerably if this is the case:
set WeapHealthFloat to GetWeaponHealthPerc * 0.01AddItemHealthpercent WeapPhalanxButchSuperSledge 1 WeapHealthFloat

Cipscis


I had tried different things and this was just what worked for me at the time. It makes more sense now that I look at it after the fact. If the getweaponhealthperc does load into the float variable okay and doesnt explicitly need a short there, what you're saying seems that it could work.

Converting short values to float sorta depends on just WHAT is meant I guess - - in the case of getweaphealthpercent it's not like you can assign one directly to the other, is all. You have to turn 90 into .90 by the * .01 deal.
User avatar
Mandy Muir
 
Posts: 3307
Joined: Wed Jan 24, 2007 4:38 pm

Post » Thu Apr 08, 2010 3:51 am

All integer values are stored internally as floating point values, such that the truncation only occurs when a value is assigned to a variable. This basically means that, for all intents and purposes, integer values can be used as if they were floating point values in expressions, which is why "GetWeaponHealthPerc * 0.01" will return a floating point value even though http://geck.gamesas.com/index.php/GetWeaponHealthPerc returns an integer value.

Cipscis
User avatar
Quick draw II
 
Posts: 3301
Joined: Thu Nov 08, 2007 4:11 pm

Post » Thu Apr 08, 2010 6:35 am

Thanks to both of you. The info, short -> float & float -> short will come in very handy. Fortunately, its quite easy.
User avatar
Monika
 
Posts: 3469
Joined: Wed Jan 10, 2007 7:50 pm


Return to Fallout 3

cron