Decimals not Working in Functions

Blitz3D Forums/Blitz3D Beginners Area/Decimals not Working in Functions

Josaih6/10(Posted 2009) [#1]
When I try to use a variable with a decimal value(in a function), it is rounded to the nearest integer. If I need to I can assign integer values and multiply by decimals, but that's not very convenient. Is there a better way?


_Skully(Posted 2009) [#2]
On your first declaration you have to tell the compiler that that you are using floats


from then on you can reference the variable without the #

local R#=0

in function calls you have to do the same...

This(0.01)

Function This(r#)
print r#
end function


Josaih6/10(Posted 2009) [#3]
Thank you!
That was exactly the kind of thing I was looking for.


Zethrax(Posted 2009) [#4]
Something else to bear in mind with floats, is that if any of the values in a math expression are float values, then the expression will be evaluated as a float expression.


GfK(Posted 2009) [#5]
Also, if you want to return a float from a function, you need to declare the function return type.

This won't work:
Print myFunc(3.14)

Function myFunc(n#)
  Return n
End Function


This will:
Print myFunc(3.14)

Function myFunc#(n#)
  Return n
End Function



_PJ_(Posted 2009) [#6]
You can also, retrieve any integer argument as a float if you are actually working with integers but need a float:

[code]
myFloat#=(Float#(Integer_A%) / (Float#(Integer_B%)))


GfK(Posted 2009) [#7]
You only need one side (doesn't matter which) to be cast as a float.