Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: Working wit...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 6 Topic 1081 of 1133
Post > Topic >>

Re: Working with Cash and uses of Typedef

by Brian Inglis <Brian.Inglis@[EMAIL PROTECTED] > Apr 18, 2008 at 04:12 PM

On Wed, 16 Apr 2008 16:57:04 -0500 (CDT) in comp.lang.c.moderated,
Philluminati <Phillip.Ross.Taylor@[EMAIL PROTECTED]
> wrote:

>I am writing a poker game which needs to work with cash. I am aware
>that there are problems with floats that make them unsuitable for
>storing money values. On top of this I may be expected to do
>operations such as =A310.52 / 3. Which data type should I use instea=
d?=20

Most code uses double, some with careful attention to rounding.=20
Some code uses scaled integers stored in double and used only in thei=
r
exact integer range, with careful attention to rounding.=20
Some code may use scaled long or long long int, with careful attentio=
n
to rounding.=20
Look for info on guard digits and rounding.=20

>Is there an Industry standard one?

Packed Decimal or Binary Coded Decimal strings which are not sup****te=
d
in C. Mike Cowlishaw has a library for decimal character digit string=
s
in C: http://www2.hursley.ibm.com/decimal/#decNumber.=20

>Further to this, is anyone away of legally what the obligations woul=
d
>be when the outcome is 3.566666667?  does the customer get 56 or 57p=
?

Look for info on bankers' rounding.=20

>Do I need to track the remaining .003333 and use it in other
>operations?

You may have to store higher precision than you output: look for info=
 on
rounding.=20

>And finally, why do I see people typedef values in C and, for exampl=
e,
>use size_t rather than an int? Why is the type hidden like this?

****tability: standard compliant compiler implementors have to provide
certain types, as they are required by the standard, and so may safel=
y
be used by ****table programs to do the right thing on multiple
platforms.=20
Certain platform standards like POSIX also specify additional types a=
nd
functions that may be available in your compiler's headers and librar=
ies
and useful in interfacing to the platform.=20

>Surely it can lead to bugs and changing a data type later in
>development can allow bitwise and maths operations to become un-
>trustable?=20

Programmer defined datatypes sup****t abstraction, which allows the
programmer to change the data definitions and sup****ting functions
without changing or even recompiling source code elsewhere, which may
involve hundreds of source modules containing megabytes of code.=20

Data and module structuring and organization is one of the hardest
lessons to learn in programming and not written about a lot.=20
Since structured programming has given way to object oriented (in som=
e
opinions, unstructured) and Pascal has become less popular, coverage =
of
programming principles e.g. in books by Wirth and papers by Dijkstra
have been neglected, although Hunt & Thomas' The Pragmatic Programmer=
,
Kernighan & Pike's The Principles of Programming, and Joel Spolsky's
collections of articles have appeared fairly recently and may still b=
e
available in some larger or technical stores. Diomedis Spinellis is a=
lso
writing an O'Reilly series on Code: Reading, Quality, ...=20

If you keep the highest compiler warning levels enabled, a compiler m=
ay
be able to tell you if operations will/may fail.=20

Choice of suitable data representations and operations keeping values
used (including tem****ary intermediates) within their valid ranges is
the ultimate responsibility of the programmer.=20

>In my own applications I have been typdef'ing structures
>merely to remove the requirement to write "struct" everywhere and I
>see how shortening the name of unsigned long to ulng would be useful
>but I don't see it's benefit in some cases. So why do people use it?

IMHO laziness: to avoid some typing at the expense of some clarity.=
=20

>I'm sorry if these questions seem stupid or simple but I'm new to C
>and I can't find that many resources for C.

The monthly FAQ postings in this group may give you more background a=
nd
references.=20

The C Standard Rationale is freely downloadable, and may explain why
certain decisions were made, although you may not understand all of i=
t
yet.=20

Books like K&R2, Harbison & Steele, Plauger's the Standard C Library =
or
anything by him, Oualline's C Elements of Style, Holub's Enough Rope =
to
Shoot Yourself in the Foot, Heathfield Kirby et al's C Unleashed, and
others recommended in the ACCU booklist below give more background,
info, and examples, and should be available thru a technical library =
or
inter-library loan.=20
http://accu.org/index.php/book_reviews?url=3Dsearch.xqy%3Fx&term=3Dbe=
ginner+AND+C&field=3Dbody

--=20
Thanks. Take care, Brian Inglis =09Calgary, Alberta, Canada

Brian.Inglis@[EMAIL PROTECTED]
 =09(Brian[dot]Inglis{at}SystematicSW[dot]ab[dot]=
ca)
    fake address=09=09use address above to reply
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 6 Posts in Topic:
Working with Cash and uses of Typedef
Philluminati <Phillip.  2008-04-16 16:57:04 
Re: Working with Cash and uses of Typedef
Carl Barron <cbarron41  2008-04-18 16:11:08 
Re: Working with Cash and uses of Typedef
Keith Thompson <kst-u@  2008-04-21 13:18:44 
Re: Working with Cash and uses of Typedef
=?ISO-8859-1?Q?Hans-Bernh  2008-04-18 16:11:16 
Re: Working with Cash and uses of Typedef
Thomas Richter <thor@[  2008-04-18 16:11:18 
Re: Working with Cash and uses of Typedef
Brian Inglis <Brian.In  2008-04-18 16:12:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:16:41 CDT 2008.