Author Topic: MMBasic - The MicoMite - Finally pushed over the edge  (Read 1841 times)

0 Members and 1 Guest are viewing this topic.

Offline g0mgxTopic starter

  • Regular Contributor
  • *
  • Posts: 69
  • Country: gb
MMBasic - The MicoMite - Finally pushed over the edge
« on: December 07, 2019, 04:09:02 pm »
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: [Select]

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

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
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: MMBasic - The MicoMite - Finally pushed over the edge
« Reply #1 on: December 07, 2019, 05:48:23 pm »
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.
 
The following users thanked this post: g0mgx

Online nali

  • Frequent Contributor
  • **
  • Posts: 657
  • Country: gb
Re: MMBasic - The MicoMite - Finally pushed over the edge
« Reply #2 on: December 07, 2019, 05:51:46 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.

Isn't that the MOD operator?
 

Offline g0mgxTopic starter

  • Regular Contributor
  • *
  • Posts: 69
  • Country: gb
Re: MMBasic - The MicoMite - Finally pushed over the edge
« Reply #3 on: December 07, 2019, 07:41:31 pm »
 |O

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

Thanks

 

Offline donotdespisethesnake

  • Super Contributor
  • ***
  • Posts: 1093
  • Country: gb
  • Embedded stuff
Re: MMBasic - The MicoMite - Finally pushed over the edge
« Reply #4 on: December 13, 2019, 03:07:59 pm »
Can anyone help me understand why, when I do 364.8 - 364 I get 0.799988

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?
Bob
"All you said is just a bunch of opinions."
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf