Convert integer to float?
dujoducom
Posts: 11
I'm sorry if this has been covered, I tried a forum search, but the search functionality seems kind of wonky sometimes and it seems like this would have to be covered.
I'm trying to do some calculations that involve floating point numbers (stuff like 0.008553) and I'm not having a lot of luck.
All my calculations were working fine until I started injecting floating point values, and now I'm getting weird values like -11784932 etc. Now I seem to have figured out that for debugging purposes I need to convert the floats to a string first, so I've been using floatstrings floattostring method (ie: fullduplexserialextended.str(fs.floattostring(0.888))) works as expected, I get the value 0.888 showing up in my terminal application running on my PC. I've been able to floating point arithmetic also with success using the float32 object, such as f32.fadd(0.5, 0.2) and debugging the value without issue. Now the problem comes when I have some integer values that I need to multiply by floating point values like the one mentioned above (0.0085553 for example). When I try this I seem to get really large numbers that don't make much sense. I've declared my own variables in some cases such as 200.0 and then it works. Now what I want to know is if there is any way to take a number such as 200 and say "Make this into a float"
Here is an example of my dream function called "ConvertToFloat"
Any ideas?
I'm trying to do some calculations that involve floating point numbers (stuff like 0.008553) and I'm not having a lot of luck.
All my calculations were working fine until I started injecting floating point values, and now I'm getting weird values like -11784932 etc. Now I seem to have figured out that for debugging purposes I need to convert the floats to a string first, so I've been using floatstrings floattostring method (ie: fullduplexserialextended.str(fs.floattostring(0.888))) works as expected, I get the value 0.888 showing up in my terminal application running on my PC. I've been able to floating point arithmetic also with success using the float32 object, such as f32.fadd(0.5, 0.2) and debugging the value without issue. Now the problem comes when I have some integer values that I need to multiply by floating point values like the one mentioned above (0.0085553 for example). When I try this I seem to get really large numbers that don't make much sense. I've declared my own variables in some cases such as 200.0 and then it works. Now what I want to know is if there is any way to take a number such as 200 and say "Make this into a float"
Here is an example of my dream function called "ConvertToFloat"
var long myval long myval2 pub main myval := 200 ' lets say that the variable HAS to start out as an integer, I can't float(myval) or put a .0 after it myval2 := float32.fmul(ConvertToFloat(myval), 0.0012345)
Any ideas?
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I am 1011, so be surprised!
Advertisement sponsored by dfletch:
Come and join us on the Propeller IRC channel for fast and easy help!
Channel: #propeller
Server: irc.freenode.net or freenode.net
If you don't want to bother installing an IRC client, use Mibbit. www.mibbit.com
Thank you once again!