Shop OBEX P1 Docs P2 Docs Learn Events
Float Math Errors?? Large, random numbers... — Parallax Forums

Float Math Errors?? Large, random numbers...

Hello,

Please forgive me if I miss any vital information, I'm still a bit new to this.

I am using the P8X32A-Q44 Microcontroller to read an inclinometer and an encoder and then spit the incoming values from both
of those to a terminal very quickly. I'm using the "F32_1_6" float math object and the "FloatString" object from the OBEX for all computations and conversion to string for displaying.

My problem is that about 1 in every 1000 readings that I get in is suddenly a huge number (always the same number), something like 6E27...
I thought maybe my fmath object was getting overloaded somehow because I'm having it loop through all the computations as fast as it can go, so I added a second fmath object running on another cog and am sending half of all the computations being done to that cog instead. This didn't seem to help, it may have lowered the number of anomalies but it's difficult to tell as it is random.

Does anyone know what is causing these sudden values? For what I'm trying to do, I need a constant stream of values coming in and they all reference that preceding value so it is very important that I do not have these anomalies in my data.

I am writing in Spin.

Thank you very much for your help,
if you need any other info just let me know

Comments

  • ElectrodudeElectrodude Posts: 1,657
    edited 2016-07-11 13:49
    You can't overload an F32 object. When you call one of its Spin methods, the method doesn't return until it's ready for the next command. Using two separate objects in the way you are using them will only make things more complicated.

    Have you tried printing out intermediate values to figure out where the numbers start being wrong?

    Are you dividing by zero anywhere?

    What is the exact huge number you're getting? FloatString can't detect NaN, and just prints out some huge number when it gets a NaN.
  • Thanks for the quick response.

    The exact value I'm getting is 5.69E+17.

    Okay, I will use just one cog for all the math. I didn't realize it did that.

    I am calling the F32 object every time I reach a certain pulse count from my encoder as it is spinning and it spins pretty fast, so I was thinking maybe I was trying to call it when it was still in the middle of the last computation. But even when I slow the encoder down or make it send less often the value will still sometimes show up. It occurs very randomly/rarely though which is strange... I tried swapping out the microcontroller too because I had no clue what could be causing it but that didn't help either.

    And yes I have tried printing all intermediate values and it doesn't always happen at the same spot. It seems to occur randomly during any one of my float calculations.
  • Since you say it seems to happen at random places, I suspect it's a stack problem. Are you manually launching a cog and not giving it enough stack space?

    If that doesn't fix it, can you give me that 5.69e17 number in hexadecimal? Don't f.ftrunc it - just pretend it's an integer, and print it out in hex. Also, I don't have a Propeller with me now to test this, but what do you get if you print out the result of "f.fdiv(1.0, 0.0)" as both a float and a hex number?
  • I am launching it on its own cog, and I just tried changing the stack from 50 to 500 but not change. When I divide 1.0 by 0.0, I get a different high value other than 5.69E+17, but interestingly when I just divide 1.0 by 2.0 looping over and over, I am getting the 5.69E+17 value sometimes.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2016-07-11 17:48
    At this point, you should probably post your code -- just the minimum to demonstrate the problem -- so we can see what's happening. I'm virtually certain that the result is getting overwritten by another cog.

    -Phil
  • Are you using F32 from multiple cogs? If so, each cog that uses F32 should have its own instance of F32 that should be used by no other cog. Otherwise, if multiple cogs make a request to the same F32 cog at the same time, they'll confuse it. A single cog can never overload a single F32 object, but it's unsafe to allow multiple cogs to use the same F32 object at the same time.
  • Thank you guys for all the help,

    Yes the error was that I was accessing the F32 from multiple cogs and it was sometimes causing an error when they overlapped.
    I created a second F32 for my second cog and it works perfectly!

    Thank you very much, you have been very helpful in solving this issue
  • Not long ago I posted a version of F32 with locks. I'm pretty sure I could find it if you're interested.

    The locks prevent issues like those you encountered without the need for a second instance of the object.
  • Duane that would be great! For what I'm trying to do, I will have to have 3 F32's going and that would leave me with no free cogs. Freeing up 2 cogs would make things much easier.
  • Here's a link to the post with the attached code. I use a stripped down version in one of my projects but I haven't tested the full version I attached to the linked post.
Sign In or Register to comment.