Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Decimal type that integrates well with Qt
Forum Updated to NodeBB v4.3 + New Features

Decimal type that integrates well with Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 968 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JohnGa
    wrote on last edited by JohnGa
    #1

    Hello,
    Does anybody have a recommendation for a library that provides a decimal type which will allow for accurate base 10 arithmetic operations? I am looking for a solution that will integrate well with QDebug, QtSql, etc. This is for Qt c++.

    Thanks.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      First, are you sure you need a fixed precision decimal type (assuming that's what you really mean), and what are your limit requirements?

      If you meet the AI on the road, kill it.

      J 1 Reply Last reply
      1
      • Kent-DorfmanK Kent-Dorfman

        First, are you sure you need a fixed precision decimal type (assuming that's what you really mean), and what are your limit requirements?

        J Offline
        J Offline
        JohnGa
        wrote on last edited by JohnGa
        #3

        @Kent-Dorfman When using doubles and float I am getting rounding errors. The calculations involve multiple steps of multiplying, adding, etc. The rounding errors seem to accumulate. I just need reliable 4 decimal places with the ability to limit to 2 decimal places.

        How can I reliably round doubles and floats to say 2 decimal places? This is not for printing, this is for numbers that will be used in calculations. I have seen the approaches that convert to ints. I will give this approach a try. Found this example in another thread:

        round(88.33500 * 100) / 100; // print 88.34

        JonBJ 1 Reply Last reply
        0
        • J JohnGa

          @Kent-Dorfman When using doubles and float I am getting rounding errors. The calculations involve multiple steps of multiplying, adding, etc. The rounding errors seem to accumulate. I just need reliable 4 decimal places with the ability to limit to 2 decimal places.

          How can I reliably round doubles and floats to say 2 decimal places? This is not for printing, this is for numbers that will be used in calculations. I have seen the approaches that convert to ints. I will give this approach a try. Found this example in another thread:

          round(88.33500 * 100) / 100; // print 88.34

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @JohnGa
          You cannot reliably do floating point arithmetic, accurate to any number of places. Floating point numbers only have approximate representations, and can only represent discrete real values. You should always use double in preference to float.

          For a fixed number of dps like 2 or 4 you can indeed do your arithmetic by multiplying up and dealing with int/long, so long as that does not exceed integer range. I haven't checked, but a C++ library like boost probably offers fixed-dp arithmetic. Qt/C++ does not offer a type to interact with SQL DECIMAL types, unfortunately, you have to roll that yourself if you want to use it.

          1 Reply Last reply
          4

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved