Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Decimal number class

Decimal number class

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 6 Posters 0 Views 1 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.
  • P Offline
    P Offline
    paulb
    wrote on last edited by
    #1

    Does anyone know of a (preferably free) class library that will accurately represent decimal numbers? we have been using floats but these have problems representing certain numbers and are resulting in rounding errors.

    T M D 3 Replies Last reply
    0
    • P paulb

      Does anyone know of a (preferably free) class library that will accurately represent decimal numbers? we have been using floats but these have problems representing certain numbers and are resulting in rounding errors.

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      could you explain a bit more ? i don't understand why you're talking about a class for integers. don't you want to use signed/unsigned int/short/long ??


      TOXCCT >>> GEII power

      P 1 Reply Last reply
      0
      • T toxcct

        could you explain a bit more ? i don't understand why you're talking about a class for integers. don't you want to use signed/unsigned int/short/long ??


        TOXCCT >>> GEII power

        P Offline
        P Offline
        paulb
        wrote on last edited by
        #3

        No I mean decimal numbers, like 1.234 float and double are no good at representing certain values (e.g. 0.1) I need a class that can accruately represent decimal numbers

        C T 2 Replies Last reply
        0
        • P paulb

          No I mean decimal numbers, like 1.234 float and double are no good at representing certain values (e.g. 0.1) I need a class that can accruately represent decimal numbers

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Instead of float (4 bytes), use double (8 bytes) or long double (10 bytes) if you need even more precision

          1 Reply Last reply
          0
          • P paulb

            No I mean decimal numbers, like 1.234 float and double are no good at representing certain values (e.g. 0.1) I need a class that can accruately represent decimal numbers

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            did you have a look at float.h ? you certainly didn't. you will find many "tools" to manage non real values like infinites, resolution loose, and so... and about your 0.1, i don't understand why float is not good...


            TOXCCT >>> GEII power

            J 1 Reply Last reply
            0
            • P paulb

              Does anyone know of a (preferably free) class library that will accurately represent decimal numbers? we have been using floats but these have problems representing certain numbers and are resulting in rounding errors.

              M Offline
              M Offline
              markkuk
              wrote on last edited by
              #6

              Here's a commercial one: Base One Number Class

              1 Reply Last reply
              0
              • T toxcct

                did you have a look at float.h ? you certainly didn't. you will find many "tools" to manage non real values like infinites, resolution loose, and so... and about your 0.1, i don't understand why float is not good...


                TOXCCT >>> GEII power

                J Offline
                J Offline
                jan larsen
                wrote on last edited by
                #7

                toxcct wrote: and about your 0.1, i don't understand why float is not good... float and double are nice containers for floating point numbers, but they aren't accurate, they can't accurately describe 2 1/3 for example. While double is ok for most apps, it would be a poor choice for eg. insurance companies where the roundings in the complex actuarial formulas would mean filtering away billions of dollars. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

                1 Reply Last reply
                0
                • P paulb

                  Does anyone know of a (preferably free) class library that will accurately represent decimal numbers? we have been using floats but these have problems representing certain numbers and are resulting in rounding errors.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Technically, decimal means base-10 (deca is Greek for ten). So, rather than using a float type, are you wanting to use an int type? If you simply need more precision, you might try the DECIMAL type. However, I suspect you are not having precision problems. Most beginners assume you can compare floating-point numbers just like you can non floating-point numbers. For example, this would never produce the desired result:

                  float a = 0.1;
                  if (a == 0.1)
                  ...

                  because a is stored internally as 0.0999999999, give or take a few 9s.


                  "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups