element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
microbit
  • Learn
  • Learning Center
  • STEM Academy
  • microbit
  • More
  • Cancel
microbit
microbit Forum Genuine decimal numbers
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join microbit to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 11 replies
  • Subscribers 49 subscribers
  • Views 2512 views
  • Users 0 members are here
  • micro:bit
  • bbc micro:bit
  • microbit
  • bbc_microbit
  • micro bit
  • bbc microbit
  • microbit.
  • microbit bbc
Related

Genuine decimal numbers

rhubarbdog
rhubarbdog over 2 years ago

if floating point numbers are letting you down because of the gaps and rounding errors try my decimal numbers class decimal.py at https://github.com/rhubarbdog/decimal-numbers . There is a version for microbit called microbit.py follow the instructions in the README.md.
See example code decimal_math.py and pi.py for python 3 and other boards and for microbit it's microbit_pi.py microbit_root.py

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 2 years ago in reply to rhubarbdog +2
    Hi, Most of us won't have a Micro:bit ready to test this immediately, so this is why I'd hoped for some example output. Sure, we could use the normal Python version instead of the Micropython version…
  • javagoza
    javagoza over 2 years ago +1
    Can you define precision and scale? Are precision and scale inferred by the constructors?
  • michaelkellett
    michaelkellett over 2 years ago +1
    I just looked at your code on Github (with a view to answering Shabaz's questions) You obviously want to share, which is great, but your code does not make it easy. decimal.py is 945 lines of code…
Parents
  • michaelkellett
    michaelkellett over 2 years ago

    I just looked at your code on Github (with a view to answering Shabaz's questions)

    You obviously want to share, which is great, but your code does not make it easy.

    decimal.py is 945 lines of code with no comments or explanation other than:

    #
    # Decimal Numbers
    # Author - Phil Hall, October 2022
    # License - MIT

    #

    Do you have some documents or notes that explain how it works and how it might be tweaked or modified or maintained ?

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • rhubarbdog
    rhubarbdog over 2 years ago in reply to michaelkellett

    Have you looked at decimal_math.py or pi.py or the microbit versions microbit_root.py and microbit_pi.py

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • michaelkellett
    michaelkellett over 2 years ago in reply to rhubarbdog

    I have, They are examples of how ot use decimal.py but they don't explain how decimal.py is actually meant to work.

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • michaelkellett
    michaelkellett over 2 years ago in reply to rhubarbdog

    I have, They are examples of how ot use decimal.py but they don't explain how decimal.py is actually meant to work.

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
  • rhubarbdog
    rhubarbdog over 2 years ago in reply to michaelkellett

    It just performs basic mathematics to a magnitude and precision only limited by memory, try

    decimal.divide(decimal.Number(1), decimal.Number(3), 1000)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 2 years ago in reply to rhubarbdog

    Hi,

    Most of us won't have a Micro:bit ready to test this immediately, so this is why I'd hoped for some example output. Sure, we could use the normal Python version instead of the Micropython version, but there will be a significant difference so that's not as useful.

    Anyway, here is a summary of Micropython floating-point for those interested. I only know this because I spent a couple of days investigating it (not on micro:bit):

    * Micropython is very similar to Python, but unfortunately some math-related libraries cannot currently work with Micropython

    * Micropython might use single-precision maths, it depends on how Micropython was compiled. I suspect it is single-precision on Micro:bit since rhubarbdog mentions 32-bit floats in an earlier comment.

    * Micropython can be re-built for double-precision, and that's highly recommended as a first step if better precision is needed, because it's seamless, no need to treat numbers differently, the result will just be more precise. Personally I wish double precision was the default for most boards, but I believe it isn't. For Pi Pico, I've built some double-precision releases of Micropython, to save the effort building them, but the effort to build it isn't hard, it is documented here (along with the links to the built images):  Pi Pico (RP2040) MicroPython Double Precision  For micro:bit someone else might have done this too, but I don't know.

    * Don't assume that Micropython calculation results will be the same as with CircuitPython, or normal Python. All are different. CircuitPython is normally the worst at it, with clear errors in places.

    * Another option is to partially code in C and partially in Python, and that way theoretically you can compute using the maths library for C, however there be dragons with that method too, in particular in some scenarios it is very hard to link to the maths library for some devices (it is to do with soft floating point for some processors, that probably won't apply to Micro:bit since that has quite a powerful chip from memory).

    * Even double-precision isn't always precise enough, hence the need for (say) quad precision. There are Python (not Micropython) libraries such as decimal.py which can provide great precision, for example on normal Python:

    >>> 100/3
    33.333333333333336

    The above is obviously not entirely correct, since it has rounded up at the end.

    In contrast, with decimal.py on normal Python:

    >>> Decimal(100)/Decimal(3)
    Decimal('33.33333333333333333333333333')

    *  The decimal.py that works on normal Python doesn't work on Micropython, partially due to some features which Micropython does not support, but although some of those can be worked around, decimal.py uses a lot of resources, and it is easy to blow the amount of RAM that is available.

    * There is a subset of functionality from the normal decimal.py that is nevertheless handy to have with MicroPython, I have tried one library which worked for me (I did not try on Micro:bit, only on Pico), it is called mpy_decimal.py and it works very well, providing as much precision as requested. The code has comments, as michaelkellett mentions comments are highly recommended otherwise it will be near-impossible for people to figure out how to improve your project, so perhaps you can use that as an example of how to do it, I feel (others may disagree) that author put in decent comments in that project. I have done a couple of spot-checks with that mpy_decimal library and the results are accurate too (more accurate than I could prove on my own, I had to compare with online Wolfram Alpha for that reassurance).

    * I could go on, but I think those are the main points that were discovered.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube