Simple Decimal class?

T

tajmorton

Hello,
First, some back ground: I am learning C++ while writing a custom
inventory program for our (seed) business. I am using Qt as my toolkit,
as the app needs to be compatible with both Windows and Linux/X11.

I have run into a problem with storing prices and weights (weights are
in pounds). Obviously, float and double won't work. I've been googleing
around, but haven't really found a easy to work with, portable, and
open source class. Does anyone have any suggestions, or should I set
about writing my own?

Thanks!
 
G

Gianni Mariani

tajmorton said:
Hello,
First, some back ground: I am learning C++ while writing a custom
inventory program for our (seed) business. I am using Qt as my toolkit,
as the app needs to be compatible with both Windows and Linux/X11.

I have run into a problem with storing prices and weights (weights are
in pounds). Obviously, float and double won't work. I've been googleing
around, but haven't really found a easy to work with, portable, and
open source class. Does anyone have any suggestions, or should I set
about writing my own?

Why does double not work ?

Is it a display issue ?

BTW - you really need to drop these lb's and go metric :)
 
T

tajmorton

Gianni Mariani said:
Why does double not work ?
Is it a display issue ?
Actually, I haven't tried double--only real. I was under the impression
that double was just a bigger real. As for why it's not working; I
can't do something like this:
if (myreal == 0.5)
Also, I've read that using real in currency is just a bad idea because
of the "floating" nature of it. What I really need is something that
when I assign it a value of 0.5--that's its exact value--no more, no
less.

Or am I completly missing something?
BTW - you really need to drop these lb's and go metric :)
Yeah, I know--I've been pushing for that. :)

Thanks for the reply,
 
R

roberts.noah

tajmorton said:
Actually, I haven't tried double--only real. I was under the impression
that double was just a bigger real. As for why it's not working; I
can't do something like this:
if (myreal == 0.5)
Also, I've read that using real in currency is just a bad idea because
of the "floating" nature of it. What I really need is something that
when I assign it a value of 0.5--that's its exact value--no more, no
less.

Often money is implemented with an integral. Instead of counting $ you
count cents. One dollar then is 100 instead of 1.00. If you can find
a boundary for your weights you could do something similar.

Now, when you do your calculations you may want to do so as reals
instead and then do some rounding:

int compute_tax(int amt) { return static_cast<int>(amt * .08 + .5); }

and so on...this will happen so often you will want a round() function
or macro.

This is where the whole Superman/Office Space thing comes into play...
 
G

Greg

tajmorton said:
Actually, I haven't tried double--only real. I was under the impression
that double was just a bigger real. As for why it's not working; I
can't do something like this:
if (myreal == 0.5)
Also, I've read that using real in currency is just a bad idea because
of the "floating" nature of it. What I really need is something that
when I assign it a value of 0.5--that's its exact value--no more, no
less.

Or am I completly missing something?

Note that a double probably could be used here, since its precision is
probably far greater than the precision needed to represent the
weights. Otherwise, a fixed width number format can be used if the
values must be exact.

First, decide on the smallest unit that needs to be represented and
treat one of those units as "1" and all other values scale from there.
So a program would not compare a particular weight against 0.5, but
against 50, assuming the scaling factor is 100. Fixed width arithmatic
is integer arithmatic, so all comparisons are exact. And the only time
when it is necessary to apply the scaling factor to the stored values
is when they are displayed. To display a fixed width value as a
floating point value, just convert the fixed point value to a doulbe
and then divide by the scaling factor (in this example, 100):

const int kScalingFactor = 100;

int weight = 50; // half a pound

printf("weight is %f", double(weight)/kScalingFactor);

Greg
 
T

tom.schultz

Greg said:
Note that a double probably could be used here, since its precision is
probably far greater than the precision needed to represent the
weights. Otherwise, a fixed width number format can be used if the
values must be exact.

First, decide on the smallest unit that needs to be represented and
treat one of those units as "1" and all other values scale from there.
So a program would not compare a particular weight against 0.5, but
against 50, assuming the scaling factor is 100. Fixed width arithmatic
is integer arithmatic, so all comparisons are exact. And the only time
when it is necessary to apply the scaling factor to the stored values
is when they are displayed. To display a fixed width value as a
floating point value, just convert the fixed point value to a doulbe
and then divide by the scaling factor (in this example, 100):

const int kScalingFactor = 100;

int weight = 50; // half a pound

printf("weight is %f", double(weight)/kScalingFactor);

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top