Home AMX User Forum AMX Technical Discussion

division not returnig float

Hi,

I don't know why this operation returns an integer value and not a float:
DEFINE_FUNCTION char[10] DbsIORealToFake(char cTipoIO, float real)
{
    stack_var float fAux
        integer nAux, nAuxTp, nAuxFake
    
    nAuxTp = MAX_LEVEL_TP - MIN_LEVEL_TP
    
    if(real > MAX_LEVEL_TP)
    real = MAX_LEVEL_TP
    if(real < MIN_LEVEL_TP)
    real = MIN_LEVEL_TP       
    
    fAux = (real - MIN_LEVEL_TP) / nAuxTp
    send_string 5001:0:0,"'fAux ',ftoa(fAux)"
}

The last line returns "0" when it should be "0.3". Does enyone can explain why is it being rounded to an integer?

Note: "real" comes from a level value

Thanks!

Comments

  • ErikMeyerErikMeyer Posts: 8
    I believe the compiler will see that the value of real is an integer (since it is from a level) and recasts during the math operation since the other operands are also integers. So you essentially have float = (integer - integer) / integer. Thus the math operation returns an integer. To get a float value back, I believe you will need to define nAuxTP as a float and create a new variable fMin and assign MIN_LEVEL_TP as follows:

    float fAuxTP
    float fMin
    
    fMIN = MIN_LEVEL_TP
    
    ...
    
    fAux = (real - fMin) / fAuxTP
    

    Now you have float = (float - float) / float which should return a float type. I have also heard, but not tried, that simply adding and then subtracting 0.1 to the equation will cause the compiler to return a float as follows:
    fAux = (real - MIN_LEVEL_TP + 0.1 - 0.1) / nAuxTp
    
  • NZRobNZRob Posts: 70
    Just wanted to check if you are talking about when you say return - if you are talking about what is returned from the function and not the send_string then you are missing the RETURN command

    e.g.
    At the end of the function you need:

    RETURN ftoa(fAux)

    Then char[10] will be something else, it will be nothing
  • MorgoZMorgoZ Posts: 116
    Ok, i had to define at least one variable at the division operands as a float and then the return type was also float, as EricMeyer said.

    Thank you both!
Sign In or Register to comment.