Shop OBEX P1 Docs P2 Docs Learn Events
Floating point to N-digit Scientific Notation string (In Spin2)? — Parallax Forums

Floating point to N-digit Scientific Notation string (In Spin2)?

RaymanRayman Posts: 13,900
edited 2022-10-27 16:54 in Propeller 2

Just updated a P1 program to display on LCD a string that was converted from a binary number (representing uTorr) to three digit scientific notation string in Torr. For example, "3.45e-2 Torr".
This was really easy to do with some code from OBEX.

Now, thinking about doing this with P2 in Spin2. Don't think we can at the moment, or can we?
I found: https://forums.parallax.com/discussion/173066/floating-point-routines-64-and-32-bit-for-p2

This looks like it would almost work, just needs something like the "SetPrecision(NumberOfDigits)" that FloatString for P1 has.

I noticed Eric notes that Spin2 now has floating point routines. Guess that makes a lot of the above redundant...

Seems like we could use just a FloatString.spin2, maybe derived from the above?

Comments

  • @Rayman said:
    Just updated a P1 program to display on LCD a string that was converted from a binary number (representing uTorr) to three digit scientific notation string in Torr. For example, "3.45e-2 Torr".
    Now, thinking about doing this with P2 in Spin2. Don't think we can at the moment, or can we?
    I found: https://forums.parallax.com/discussion/173066/floating-point-routines-64-and-32-bit-for-p2

    This looks like it would almost work, just needs something like the "SetPrecision(NumberOfDigits)" that FloatString for P1 has.

    Yes, you basically just need to modify the SendFloatSci() method to have a parameter to specify the number of digits (or to take it from a global variable). The main change is the "repeat DEC_MAX_DIGITS" loop would have to loop for the appropriate number of digits instead. The tricky part would be rounding; for the last digit you'd have to check the remaining decval to see if you need to round up, and come to think of it you'd have to do that before you start to output because you can't go back and change earlier digits. Ugh. That's probably why I didn't try to do that. If you don't mind the output being rounded towards 0 then you could just ignore the issue and pretty much use the code as-is, just with a smaller loop count.

    Or, you could port FloatString.spin to Spin2. If you're using FlexSpin that's not an issue, since FlexSpin can compile Spin1 for the P2.

  • "since FlexSpin can compile Spin1 for the P2."

    Yes, @ersmith this is one of the outstanding features of you project.

    Thanks for your work,

    Mike

Sign In or Register to comment.