PDA

View Full Version : how?: if a float=0.0 ..then set float=1.0


cs1.7
06-28-2010, 14:31
hi

how can i force "floatX" to be 1.0 ..if has a value of 0.0?

this does not work:

new Float:floatX = variableM * 100 / g_AlivePlayers;
if(floatX = 0.0)
{
floatX = 1.0;
}

YamiKaitou
06-28-2010, 14:39
= means set
== means "is equal to"

mottzi
06-28-2010, 14:57
new Float:floatX = variableM * 100 / g_AlivePlayers;
if(floatX == 0.0)
{
floatX = 1.0;
}

cs1.7
06-28-2010, 15:07
actually that can be considered as a typo.

i do have :

if(floatX == 0.0)
{
floatX = 1.0;
} and it's not working.

Sylwester
06-28-2010, 15:16
Floats are not like integers. Results of formulas rarely equal 0.0, so use something like this:

#define FLOAT_ZERO 0.0001 //anything lower than this is considered 0.0
if(-FLOAT_ZERO <= floatX <= FLOAT_ZERO)
{
floatX = 1.0;
}

cs1.7
06-28-2010, 15:27
thx..

well if g_AlivePlayers = 0 ..then the outcome of the formula is 0

will your method work then?


edit:

another question:

if variable is >30.0 --> set it to 30.0
if it is <15.0 -->set it to 15.0

how do i create such a code most efficiently? Can i create this via one if sentence?

here i need two if conditions to achieve my goal:
if(variableX > 30.0)
{
variableX = 30.0;
}
else
{
variableX = MAXm / 400.0;
}

if(variableX < 15.0)
{
variableX = 15.0;
}
else
{
variableX = MAXm / 400.0;
}

fysiks
06-28-2010, 18:36
thx..

well if g_AlivePlayers = 0 ..then the outcome of the formula is 0


Dividing by zero will result in an error.


edit:

another question:

if variable is >30.0 --> set it to 30.0
if it is <15.0 -->set it to 15.0

how do i create such a code most efficiently? Can i create this via one if sentence?



new Float:variable
variable = floatclamp(variable, 15.0, 30.0)



here i need two if conditions to achieve my goal:
if(variableX > 30.0)
{
variableX = 30.0;
}
else
{
variableX = MAXm / 400.0;
}

if(variableX < 15.0)
{
variableX = 15.0;
}
else
{
variableX = MAXm / 400.0;
}



I don't understand this code.

cs1.7
06-28-2010, 19:11
Dividing by zero will result in an error.


hmm.. it didnt. the formula returned a value of 0 if aliveplayers were 0.

I don't understand this code.

how can the below code made more efficient?
new Float: variableX = MAXm / 400;

if (variableX > 30.0)
{
variableX = 30.0;
}

if (variableX < 15.0)
{
variableX = 15.0;
}

fysiks
06-28-2010, 20:34
variableX = floatclamp( MAXm/400.0, 15.0, 30.0 )