 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
tajmorton Guest
|
Posted: Wed Nov 30, 2005 5:40 am Post subject: Simple Decimal class? |
|
|
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!
--
Taj
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Wed Nov 30, 2005 5:47 am Post subject: Re: Simple Decimal class? |
|
|
tajmorton wrote:
| Quote: | 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 :-)
|
|
| Back to top |
|
 |
tajmorton Guest
|
Posted: Wed Nov 30, 2005 6:07 am Post subject: Re: Simple Decimal class? |
|
|
Gianni Mariani <gi2nos... (AT) mariani (DOT) ws> wrote:
| Quote: | 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?
| Quote: | 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,
--
Taj
|
|
| Back to top |
|
 |
roberts.noah@gmail.com Guest
|
Posted: Wed Nov 30, 2005 6:28 am Post subject: Re: Simple Decimal class? |
|
|
tajmorton wrote:
| Quote: | Gianni Mariani <gi2nos... (AT) mariani (DOT) ws> wrote:
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.
|
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...
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Wed Nov 30, 2005 7:14 am Post subject: Re: Simple Decimal class? |
|
|
tajmorton wrote:
| Quote: | Gianni Mariani <gi2nos... (AT) mariani (DOT) ws> wrote:
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?
|
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
|
|
| Back to top |
|
 |
tom.schultz Guest
|
Posted: Wed Nov 30, 2005 7:36 am Post subject: Re: Simple Decimal class? |
|
|
Greg wrote:
| Quote: | tajmorton wrote:
Gianni Mariani <gi2nos... (AT) mariani (DOT) ws> wrote:
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?
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
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|