Shop OBEX P1 Docs P2 Docs Learn Events
Float question — Parallax Forums

Float question

The Float command in spin is easy enough, but round and truncate have got me scratching my head.
How do you convert floats back to integer in spin?
Surely this has come up before. Any links to where this has allready been discussed?

Comments

  • bambino69 wrote: »
    The Float command in spin is easy enough,

    As far as I know, the "float" command in spin is only used at compile time. To convert floats and integers at runtime requires a dedicated object such as F32.

    I'm not aware of a F32 equivalent for Spin2.

    I've searched the Propeller 2 forum on this topic and all I've learned is the P2 doesn't have built in floating point hardware (at least the thread from 2014 said this).

    Hopefully someone more knowledgeable will set us straight.



  • There is a really old P1 library called FloatMath that could probably be upgraded to Spin2 pretty easily (in fact, I may grab a coffee and give it a go).

    I've attached in case you don't have it on your system.
  • JonnyMac wrote: »
    ....(in fact, I may grab a coffee and give it a spin).....
    LOL - surely that's what you meant to type?

    Doc
  • Indeed! Have P1 and P2 running on my desk to compare one to the other. With inline PASM, it should make for a pretty nice object for the P2. That said, I may reach out to Chip for help as he wrote the original.
  • I know ozpropdev has been working on a conversion of Floatmath and Float32. I think from what he said a few weeks ago most of it converted well but there was one more challenging area
  • RaymanRayman Posts: 14,609
    If you need floating point, I’d seriously consider flexprop C or Catalina C.
  • Here's what I ended up with porting FloatMath to Spin2.
    Differences are the ENCOD operation returns different values to P1.
    SAR and SCA were substituted.
    IIRC thats all I changed and it seems to work Ok.
  • Thanks, Brian.
  • ersmithersmith Posts: 6,045
    edited 2020-11-01 00:59
    Rayman wrote: »
    If you need floating point, I’d seriously consider flexprop C or Catalina C.

    Indeed. Or flexprop BASIC; that also has full floating point support, and it's got a very simple syntax:
    var x = 1.2
    var y = 2.3
    print x+y
    
  • RaymanRayman Posts: 14,609
    I'm finding that printf() in C is wonderful, especially with floating point.
  • Go Team Oz!
  • ersmith wrote: »
    Rayman wrote: »
    If you need floating point, I’d seriously consider flexprop C or Catalina C.

    Indeed. Or flexprop BASIC; that also has full floating point support, and it's got a very simple syntax:
    var x = 1.2
    var y = 2.3
    print x+y
    

    😎👍👍
  • bambino69bambino69 Posts: 126
    edited 2020-11-03 02:32
    Indeed. Or flexprop BASIC; that also has full floating point support, and it's got a very simple syntax:

    I've registered for the live forum this week. Y'all can take your best shot at me.
    I love the spin language, and the prop ide will get sweeter everyday.
    With the p1 it was all about time to market for me, and I didn't really get to play like I wanted to.
    This time around I just want to keep it fun.

    Learning another language at the same time I'm re-learning spin might put my lid in orbit.
    But just like catalina, and c, and forth was to the p1, I do want to know where you guys are.

    My pursuit at the moment is to maybe help update some p1 objects so I'm assuming these other Ide's would not benefit me
    in that endeavor. Someone correct me if I'm wrong.
  • I'm with you; I like that for those who don't know Spin there are choices, but I have always liked Spin and have a lot of P1 code that is [mostly] easy to port to the P2. Programming the P2 in Spin2 is a lot of fun, and in time there will be a lot of nice Spin2 libraries for those of us who prefer that language.
  • bambino69 wrote: »
    Indeed. Or flexprop BASIC; that also has full floating point support, and it's got a very simple syntax:

    My pursuit at the moment is to maybe help update some p1 objects so I'm assuming these other Ide's would not benefit me
    in that endeavor. Someone correct me if I'm wrong.

    FlexProp can compile Spin 1 to run on the P2, so it saves some work in porting. This isn't a huge deal (porting from Spin 1 to Spin 2 is not difficult) but if you have a lot of Spin code and you don't want to do the mechanical work of changing keywords and putting parentheses in where you need to, it may save you some effort. The PASM parts of your code still have to be converted, since P1 and P2 have different assembly languages.

    Different languages do have different strengths. If you're doing a lot of floating point work, using a language like C or BASIC that has built in floating point lets you write things like "x + y * z" instead of "f.FAdd(x, F.FMul(y, z))". With FlexProp you can even mix the Spin with BASIC or C, so you could write the high level floating point part in BASIC while leaving the low level device drivers in Spin.
  • Looking forward to your Presentation Eric, See you soon!
  • JonnyMac wrote: »
    There is a really old P1 library called FloatMath that could probably be upgraded to Spin2 pretty easily

    FloatMath.spin compiles without errors in FlexSpin and works on a P2, as far as I can tell.

  • bambino69bambino69 Posts: 126
    edited 2020-11-03 17:29
    ozpropdev wrote: »
    Here's what I ended up with porting FloatMath to Spin2.
    Differences are the ENCOD operation returns different values to P1.
    SAR and SCA were substituted.
    IIRC thats all I changed and it seems to work Ok.

    http://forums.parallax.com/discussion/download/132484/FloatMath_2.spin2
    I've tried a couple tests with this object: so far its rock solid.(Haven't tested everything yet, but this will pretty much cover it)

  • FloatMath.spin compiles without errors in FlexSpin and works on a P2, as far as I can tell.
    Many of us are not using FlexSpin. It's nice to have the libarary up-to-date for the P2, anyway, regardless of tool used.
Sign In or Register to comment.