Products > Embedded Computing

MMBasic - The MicoMite - Finally pushed over the edge

(1/1)

g0mgx:
Hi Gang

I've been developing some code on the micromite:

https://micromite.org/micromites/

It is designed to use MMBasic and I have writing for my target which is a PIC32MX170F256B.

My recent code issues have boiled down to some basic maths that isn't producing the results I expect.  |O Simple example below:  :palm:


--- Code: ---
SUB DoStuff

  LOCAL AS INTEGER INT_A
  LOCAL AS FLOAT FLOAT_B
  LOCAL AS INTEGER INT_C
  LOCAL AS FLOAT FLOAT_D

  INT_A = 456 * 8               'expect 3648
  FLOAT_B = INT_A / 10          'expect 364.8
  INT_C = FIX(FLOAT_B)          'expect 364
  FLOAT_D = FLOAT_B - INT_C     'expect 0.8
 
  print "INT_A" ; INT_A         ' this prints 3648
  PRINT "FLOAT_B" ; FLOAT_B     ' this prints 364.8
  PRINT "INT_C" ; INT_C         ' this prints 364
  PRINT "FLOAT_D" ; FLOAT_D     ' this prints 0.799988 !?!?!?!

ENDSUB

--- End code ---

I'm actually calculating register values for a PLL, but I have traced my issues to the inacuracies I reproduce above.

Can anyone help me understand why, when I do 364.8 - 364 I get 0.799988

Grateful for any sanity saving pointers or perhaps a better way to just get the remainder from a real number division. The FIX function gives me the quotient but I cant find the function to give me the remainder.

Thanks

andersm:
Floating point math is not exact. The value 0.8 can't be expressed using standard IEEE754 floating point. There's a ton of material available on the subject, including guides on best practices when using floating point numbers in your code.

nali:

--- Quote from: g0mgx on December 07, 2019, 04:09:02 pm ---Grateful for any sanity saving pointers or perhaps a better way to just get the remainder from a real number division. The FIX function gives me the quotient but I cant find the function to give me the remainder.

--- End quote ---

Isn't that the MOD operator?

g0mgx:
 |O

I was looking for a math function, MOD is indeed there, but an operator.

Thanks

donotdespisethesnake:

--- Quote from: g0mgx on December 07, 2019, 04:09:02 pm ---Can anyone help me understand why, when I do 364.8 - 364 I get 0.799988

--- End quote ---

Because that is the accuracy you get with FLOAT. https://en.wikipedia.org/wiki/Floating-point_arithmetic

The real question is why are you using FLOAT for integer calculations?

Navigation

[0] Message Index

There was an error while thanking
Thanking...
Go to full version