AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   how?: if a float=0.0 ..then set float=1.0 (https://forums.alliedmods.net/showthread.php?t=130875)

cs1.7 06-28-2010 14:31

how?: if a float=0.0 ..then set float=1.0
 
hi

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

this does not work:

PHP Code:

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


YamiKaitou 06-28-2010 14:39

Re: how?: if a float=0.0 ..then set float=1.0
 
= means set
== means "is equal to"

mottzi 06-28-2010 14:57

Re: how?: if a float=0.0 ..then set float=1.0
 
PHP Code:

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


cs1.7 06-28-2010 15:07

Re: how?: if a float=0.0 ..then set float=1.0
 
actually that can be considered as a typo.

i do have :

PHP Code:

  if(floatX == 0.0)
  {
    
floatX 1.0;
  } 

and it's not working.

Sylwester 06-28-2010 15:16

Re: how?: if a float=0.0 ..then set float=1.0
 
Floats are not like integers. Results of formulas rarely equal 0.0, so use something like this:
PHP Code:

#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

Re: how?: if a float=0.0 ..then set float=1.0
 
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:
PHP Code:

    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

Re: how?: if a float=0.0 ..then set float=1.0
 
Quote:

Originally Posted by cs1.7 (Post 1222788)
thx..

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

Dividing by zero will result in an error.

Quote:

Originally Posted by cs1.7 (Post 1222788)
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?

Code:

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

Quote:

Originally Posted by cs1.7 (Post 1222788)
here i need two if conditions to achieve my goal:
PHP Code:

    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

Re: how?: if a float=0.0 ..then set float=1.0
 
Quote:

Originally Posted by fysiks (Post 1223002)
Dividing by zero will result in an error.

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

Originally Posted by fysiks (Post 1223002)
I don't understand this code.

how can the below code made more efficient?
PHP Code:

    new FloatvariableX  MAXm 400;
    
    if (
variableX  30.0
    {
        
variableX  30.0;
    }
    
    if (
variableX  15.0)
    {
        
variableX  15.0;
    } 


fysiks 06-28-2010 20:34

Re: how?: if a float=0.0 ..then set float=1.0
 
Code:

variableX = floatclamp( MAXm/400.0, 15.0, 30.0 )


All times are GMT -4. The time now is 14:51.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.