Shop OBEX P1 Docs P2 Docs Learn Events
Convert integer to float? — Parallax Forums

Convert integer to float?

dujoducomdujoducom Posts: 11
edited 2008-09-30 15:42 in Propeller 1
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"

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

  • tpw_mantpw_man Posts: 276
    edited 2008-09-29 22:54
    I think you can do 200.0 and it will convert it into standard floating point. FloatMath has an FFloat function that does what you want.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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
    tongue.gif
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-29 23:10
    The Spin compiler does allow you to write floating point constants (like 200.0) which are treated as long integers.· The compiler also allows constant floating point expressions in CON sections and within CONSTANT() expressions that are evaluated at compile time.· Other than that, you have to use some kind of floating point library like floatmath or float32.· The ordinary operators like "+", "*", etc. operate on integer values.· If you use them on floating point values, you'll get the result of performing the integer operation on the internal format of the floating point values.
  • dujoducomdujoducom Posts: 11
    edited 2008-09-30 15:31
    OK, I understand that you can add the .0 or declare constants as floats, and I also understand that you can't do float calculations with the standard + - * operators. I guess my question is do the float32 operators accept ONLY floats as input, or can you mix integers and floats like: float32.fadd(1.923, 20). And if you can't mix integers and floats, then how would I make a variable that is an integer compatible with those? The floatmath.ffloat looks promising, I'll check it out.

    Thank you once again!
  • dujoducomdujoducom Posts: 11
    edited 2008-09-30 15:42
    Ah hah! floatmath.ffloat() did the trick, which I think answers my question, you can not do float32 fadd fsub fdiv fmul etc methods on integers without first converting them to floats.
Sign In or Register to comment.