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 2515 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…
  • shabaz
    shabaz over 2 years ago

    Hi,

    It may be worth pasting in some output from an example or two on this discussion post, just to show the difference, so people can more easily understand the benefits.

    For instance, an error without the class, and the difference with it. I appreciate people could run the example code, but that entails them making the effort, and some people will not, unless they see some example output.

    A minor point (perhaps worth it for a later code revision) could be to change the name of it, because decimal.py already exists for normal Python, and it may get confusing for people if they expect it to be the same (or similar) thing.

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

    try this....

    import decimal

    decimal.Number(1) == decimal.Number(1.0)

    one = decimal.Number(1)

    two = decimal.Number(2)

    eleven = decimal.Number(11)

    pi = decimal.Number(3.1415)

    minus_one = decimal.Number(-1)

    print(decimal.add(one, two))

    print(decimal.subtract(one, two))

    print(decimal.multiply(eleven, two))

    print(decimal.divide(one, eleven, 15))

    print(decimal.absolute(minus_one))

    print(decimal.integer(pi))

    print(decimal.fraction(pi))

    print(decimal.round(pi, 3))

    # the correct way to express a = b

    new_number = decimal.copy(one)

    print(new_number)

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

    Can you define precision and scale? Are precision and scale inferred by the constructors?

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

    I mean, what is the output? Can you demonstrate the error without using the library, versus with using the library?

    I was hoping, something like this (this isn't a real example, this is just to provide a rough idea of what I mean):

    Without decimal-numbers library:

    >>100 / 3

    >> 33.330

    With decimal-numbers library:

    >>print(decimal.divide(decimal.integer(100), decimal.integer(3))

    >> 33.3333333333

    Also, are you using micropython with single-precision, or micropython with double-precision? I have not used micro:bit in a very long time, so I am not sure what it has by default (the micropython firmware is built for single or double precision, I just don't know which of the two the micro:bit firmware image is built to).

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • 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 javagoza

    They are implied by the precision of the factors in each function or creation of a new number, except division which you decare the precision.

    On a microbit I performed 1/3 to 5000 places without running out of memory 

    • Cancel
    • Vote Up 0 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
  • rhubarbdog
    rhubarbdog over 2 years ago in reply to shabaz

    On python 3 decimal.Number (0.115) != decimal.Number ("0.115") but on microbit they are the same.

    It is only for version 2 microbit''s version 1 have a code limit of about 8kb

    With the decimal numbers library try

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

    The result will be 33.3333.....

    A much greater precision than both python 3 and the 32 bit floats that are implemented in microbit micropython.

    See examples of it in use in decimal_math.py and pi.py or for microbiology it's microbit_root.py and microbit_pi.py 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • 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
>
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