division not returnig float
MorgoZ
Junior Member
Hi,
I don't know why this operation returns an integer value and not a float:
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!
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
-
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
-
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
-
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!
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums