473,887 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Working with Cash and uses of Typedef

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 £10.52 / 3. Which data type should I use instead?
Is there an Industry standard one?

Further to this, is anyone away of legally what the obligations would
be when the outcome is 3.566666667? does the customer get 56 or 57p?
Do I need to track the remaining .003333 and use it in other
operations?

And finally, why do I see people typedef values in C and, for example,
use size_t rather than an int? Why is the type hidden like this?
Surely it can lead to bugs and changing a data type later in
development can allow bitwise and maths operations to become un-
trustable? 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?

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.

Phillip Taylor
Jun 27 '08 #1
18 1445
Philluminati 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 £10.52 / 3. Which data type should I use instead?
Is there an Industry standard one?
Depending on the range of values you need to work with, use
either float or double.
Further to this, is anyone away of legally what the obligations would
be when the outcome is 3.566666667? does the customer get 56 or 57p?
Do I need to track the remaining .003333 and use it in other
operations?
Choose your rounding strategy and make it public so that players
know how things work. Making the rules of your game public is
probably more important than the particular strategy.
And finally, why do I see people typedef values in C and, for example,
use size_t rather than an int? Why is the type hidden like this?
size_t is an unsigned integer type, which may or may not resemble
an unsigned int - while an int is a signed integer type which may
not be large enough to hold the values required of a size_t.
Surely it can lead to bugs and changing a data type later in
development can allow bitwise and maths operations to become un-
trustable? 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?
It's a communication mechanism that allows using a simple
descriptive name for <somethingtha n perhaps could otherwise be
applied. Sometimes it's used for emphasis or to stress how a
programmer intends something to be used.
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.
Certainly not stupid and, if you hang around here, you'll
discover that even "simple" questions can provoke serious
discussion of underlying nuance. :-)

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 27 '08 #2
On Apr 16, 7:11 am, Philluminati <Phillip.Ross.T ay...@gmail.com >
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 £10.52 / 3. Which data type should I use instead?
Is there an Industry standard one?
The problems with floats and doubles is that some values cannot be
represented *exactly* in a finite number of bits, so errors in
calculations can propagate. This becomes more of an issue when you
start working with very big or very small numbers; for the cash values
in a typical poker game, this shouldn't be too much of a problem.

One alternative is to use an integer type scaled to the precision you
desire. For example, if you need to track down to the penny, 1 == 1
penny, so 100 = $1.00. If you need to track tenths of pennies, then 1
== 1/10 penny, so 1000 = $1.00. The problem with this is you're
limiting the range of values you can represent. If you're tracking
pennies, a 16-bit int can represent values in the range -$327.68 to
$327.68. If you're tracking tenths of pennies, that same 16 bit int
can only represent the range -$32.768 to $32.768. A 32-bit int gives
you more room to work with (-$2,147,483.648 to $2,147,483.648 for
tents of pennies), but ultimately you run into the same problem.

Another solution is to use two int types, one to track whole dollar
amounts, the other to track pennies (or fractions of pennies). This
is obviously a lot more work, and for what you want to do, probably
not worth it.
Further to this, is anyone away of legally what the obligations would
be when the outcome is 3.566666667? does the customer get 56 or 57p?
Do I need to track the remaining .003333 and use it in other
operations?
If you're rounding to pennies, then that would round to 57p;
And finally, why do I see people typedef values in C and, for example,
use size_t rather than an int? Why is the type hidden like this?
Portability. size_t is the type of the sizeof operator, and depending
on the implementation, it may be the same size as unsigned int,
unsigned long, unsigned long long, etc. By abstracting away that
implementation detail, it frees us from having to worry about it if we
port our code.
Surely it can lead to bugs and changing a data type later in
development can allow bitwise and maths operations to become un-
trustable?
As long as it's an integral type (which it's supposed to be), then
those bitwise and math operations should stay the same.
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?
Abstraction. Information hiding. Sometimes you don't want to expose
the details behind an abstract data type to a programmer. Consider
the FILE type in stdio.h; this is a typedef that encapsulates I/O
state data (I/O channel, position within stream, error flags, etc.).
Instead of manipulating this data directly, we simply pass FILE
pointers to the various stdio routines. This has the following
benefits:

* Portability -- we don't have to adjust our code for
different platforms;

* Reliability -- by not allowing the programmer to manipulate
state directly, there's less risk of
error;

* Ease of use -- calling fopen() and printf() and scanf() are
a hell of a lot easier than manipulating I/O
channels directly.

Abstraction is a powerful tool, and when done properly can make life
significantly easier.
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.

Phillip Taylor
These are not stupid questions; they're very good questions. One good
resource is the FAQ: http://www.c-faq.com/. Two of the more
authoritative references are "The C Programming Language", 2nd ed., by
Kernighan & Ritchie, and "C: A Reference Manual", 5th ed., by Harbison
& Steele.
Jun 27 '08 #3
Philluminati <Ph************ *****@gmail.com writes:
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 £10.52 / 3. Which data type should I use instead?
Is there an Industry standard one?
Other posters have covered this reasonably well.
Further to this, is anyone away of legally what the obligations would
be when the outcome is 3.566666667? does the customer get 56 or 57p?
Do I need to track the remaining .003333 and use it in other
operations?
This is emphatically the wrong place to ask for legal advice.
And finally, why do I see people typedef values in C and, for example,
use size_t rather than an int? Why is the type hidden like this?
Surely it can lead to bugs and changing a data type later in
development can allow bitwise and maths operations to become un-
trustable?
You wouldn't normally perform bitwise operations on values of type
size_t. Values of type size_t are used to store the sizes of things.
The underlying type *needs* to change on different implementations
that can support larger or smaller objects. If you write your code
properly, it will continue to work.
In my own applications I have been typdef'ing structures
merely to remove the requirement to write "struct" everywhere
Many people do that, and there's nothing wrong with it; personally, I
prefer to drop the typedef and write "struct" everywhere. Keystrokes
are cheap.
and I
see how shortening the name of unsigned long to ulng would be useful
You do? I don't. If I see "ulng" in your code, I have to go
somewhere else to figure out what it means. If I see "unsigned long",
there's no doubt that you're referring to the predefined type.
but I don't see it's benefit in some cases. So why do people use it?
Sometimes it makes sense to hide the details of a type from the code
that uses it. See, for example, the type FILE declared in <stdio.h>.
You don't need to know whether it's a struct or something else; all
you need to konw is that you can declare pointers to it and pass them
to the predefined functions.
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 FAQ at <http://www.c-faq.com/is an excellent resource and has
pointers to other good resources. K&R2 (Kernighan & Ritchie, "The C
Programming Language", 2nd edition) is widely considered to be the
best book on C (perhaps on any language).

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #4

"Phillumina ti" <Ph************ *****@gmail.com wrote in message
news:10******** *************** ***********@s50 g2000hsb.google groups.com...
>Further to this, is anyone away of legally what the obligations would
be when the outcome is 3.566666667? does the customer get 56 or 57p?
The customer might be more concerned with the missing £3.00..

This is similar to rounding problems with VAT (17.5% of 150p is 26.25p);
whatever they do, you can adopt the same.

And there are likely to be discrepancies: if you round a dozen amounts then
sum, the total could be different from summing first then rounding. You need
to do be a bit more research somewhere other than c.l.c.
>Do I need to track the remaining .003333 and use it in other
operations?
When I did some similar stuff, accounts were stored in whole cents, even
though intermediate calculations may have used fractional cents. After all,
incoming and outgoing revenues were always in whole cents.

-- Bartc

Jun 27 '08 #5
On Apr 16, 5:11*am, Philluminati <Phillip.Ross.T ay...@gmail.com >
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 £10.52 / 3. Which data type should I use instead?
Is there an Industry standard one?
http://www2.hursley.ibm.com/decimal/
Further to this, is anyone away of legally what the obligations would
be when the outcome is 3.566666667? *does the customer get 56 or 57p?
Do I need to track the remaining .003333 and use it in other
operations?
If it is a gambling outfit, and they are soaking the crap out of the
poor smucks that play online, why not just go offshore to some little
island with no sensible laws and really rip them off.
And finally, why do I see people typedef values in C and, for example,
use size_t rather than an int?
A size_t and an int are quite different. For instance, size_t is
unsigned and can represent the size of any C object. You have no such
guarantee with int.
Why is the type hidden like this?
It's called an abstraction. Abstractions are created to make code
more portable. The symbolic meaning of size_t should be obvious: it's
an ideal unsigned integral value that can successfully represent all
sizes.
Surely it can lead to bugs and changing a data type later in
development can allow bitwise and maths operations to become un-
trustable?
Strike that, reverse it.
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.
Using a typedef on a struct is pretty much just a convenience. For C+
+ programmers, it is a little closer to home so you will see a lot
more of them doing it.
So why do people use it?
To save a few keystrokes (for struct typedefs).
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.
Have you read the FAQ?
There are some good book suggestions in the FAQ as well.
Jun 27 '08 #6
Morris Dovey wrote, On 16/04/08 16:03:
Philluminati 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 £10.52 / 3. Which data type should I use instead?
Is there an Industry standard one?

Depending on the range of values you need to work with, use
either float or double.
That is asking for trouble, especially with someone inexperienced enough
to be asking the question.

The OP should store the values as pennies in an int or long depending on
the required range (I'm assuming that negative money is valid as it
often is).
>Further to this, is anyone away of legally what the obligations would
be when the outcome is 3.566666667? does the customer get 56 or 57p?
Do I need to track the remaining .003333 and use it in other
operations?

Choose your rounding strategy and make it public so that players
know how things work. Making the rules of your game public is
probably more important than the particular strategy.
Agreed.

<snip>
>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.

Certainly not stupid and, if you hang around here, you'll
discover that even "simple" questions can provoke serious
discussion of underlying nuance. :-)
As evidence we already have a disagreement :-)
--
Flash Gordon
Jun 27 '08 #7
Flash Gordon wrote:
>
Morris Dovey wrote, On 16/04/08 16:03:
Philluminati 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 £10.52 / 3. Which data type should I use instead?
Is there an Industry standard one?
Depending on the range of values you need to work with, use
either float or double.

That is asking for trouble, especially with someone inexperienced enough
to be asking the question.
Hopefully not. What /might/ be asking for trouble is for an
inexperienced programmer to implement any kind of financial
(sub)system without help from experienced folks...
The OP should store the values as pennies in an int or long depending on
the required range (I'm assuming that negative money is valid as it
often is).
When the Philadelphia Stock Exchange added decimal trading in
2001, there was considerable debate over currenty representation.
After a _lot_ of really nit-picky analysis, we settled on double.
Before bringing the decimal trading on line, we stress tested all
the standard library functions that worked on or returned
doubles, and we _did_ find bugs - Philluminati beware!
As evidence we already have a disagreement :-)
Not really - I'm perfectly happy for you to represent cash
however it works for you. May you have enough of it to find cause
to worry about the number of significant digits in a double! :-D

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 27 '08 #8
Flash Gordon wrote:
The OP should store the values as pennies in an int or long depending on
the required range (I'm assuming that negative money is valid as it
often is).
A lot of people say that, but IME they tend to be people who don't work
in banking. I've been working in the City (of London) for 20 years now
and can't recall seeing a system (e-trading, stock exchange, deal
capture, risk management or back office) that actually worked in pennies.

What you /do/ see is data interchange formats that work without the
decimal point. It can be a pig when you've got eg Eurobonds and USTs in
the same feed - the former are measured to the nearest penny, the latter
to the nearest 32nd (or 128th). Usually you have a flag somewhere else
in the file to tell you where to insert the decimal, and how to treat
the RHS (eg 10131 if its a UST means 101 and 31/32nds, whereas for a
eurobond it would mean 101.31)
>>Further to this, is anyone away of legally what the obligations would
be when the outcome is 3.566666667? does the customer get 56 or 57p?
Do I need to track the remaining .003333 and use it in other
operations?

Choose your rounding strategy and make it public so that players
know how things work. Making the rules of your game public is
probably more important than the particular strategy.

Agreed.
Agreed too - though there are several common strategies which you should
consider as people will understand them:
- round up if the significant digit is 5 or larger eg 1.005 -1.01
- round up if the integer part is even eg 1.005 -1.00, 2.005 -2.01
- truncate at the Nth decimal irrespective. 1.00999999 -1.00
>>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.
These questions aren't really C specific however - comp.programmin g
would probably be as good a place.
Jun 27 '08 #9
Morris Dovey wrote, On 16/04/08 23:38:
Flash Gordon wrote:
>Morris Dovey wrote, On 16/04/08 16:03:
>>Philluminat i 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 £10.52 / 3. Which data type should I use instead?
Is there an Industry standard one?
Depending on the range of values you need to work with, use
either float or double.
That is asking for trouble, especially with someone inexperienced enough
to be asking the question.

Hopefully not. What /might/ be asking for trouble is for an
inexperienced programmer to implement any kind of financial
(sub)system without help from experienced folks...
Well, we are talking about someone working on a system dealing with
monetary values...
>The OP should store the values as pennies in an int or long depending on
the required range (I'm assuming that negative money is valid as it
often is).
When the Philadelphia Stock Exchange added decimal trading in
2001, there was considerable debate over currenty representation.
After a _lot_ of really nit-picky analysis, we settled on double.
I know it can be done, I also know how easy it is to hit problems...
Before bringing the decimal trading on line, we stress tested all
the standard library functions that worked on or returned
doubles, and we _did_ find bugs - Philluminati beware!
and you did hit problems :-)

I'm sure you also worked out how to handle them appropriately.
>As evidence we already have a disagreement :-)

Not really - I'm perfectly happy for you to represent cash
however it works for you. May you have enough of it to find cause
to worry about the number of significant digits in a double! :-D
That's not a problem, if I hit that level I will be able to afford to
buy a computer with more significant digits and get the SW written for
it :-)
--
Flash Gordon
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6326
by: Scott A. Keen | last post by:
Hi, I'm getting tasked with writing an ASP application deployed throughout the company's intranet where all the workstations will be running Internet Explorer 6.0. It's a retail business, and some of the workstations are Point-Of-Sale and will need to have cash drawers and bar-code scanners. The project managers want this as a web-application because of the ease of
0
1928
by: Shakil Shamji | last post by:
I have a VB.Net application which is deployed on Citrix. It uses Crystal Report 8.5 for printing reports. One of my remote users has an Indiana Cash Drawer connected to the printer port. When the crystal report is printed the user wants her cash drawer to open. Can anyone give me ideas as to how can I send a non-printable charater like chr(33) to the drawer via the crystal report or otherwise to open the drawer? Thanks. Shakil.
1
4215
by: Earl Anderson | last post by:
My brother is in the process of purchasing a neighborhood dry cleaners store. Having seen some of the process applications I've written in MS Access, he asked me if I could develop an application to use in his new dry cleaning store since the existing one is of 1988 vintage. I told him that although I thought the 'process' involved in a dry cleaners couldn't be too complex and probably could be developed in Access, I had absolutely no...
1
495
by: Amos | last post by:
Dear Sirs I am trying to build a cash flow software, first I thought to build one table for each cash and bank account, but talking to some people they suggested me to build one unique table for all bank and cash accounts, putting one more column to identify each bank or cash account. The unique table has this columns structure: ID Date B/C account FinCode Description Debit Credit
17
2848
by: Jonathan Burd | last post by:
Greetings everyone, Reading about the int64_t types added by C'99, I decided to implement a 64-bit version of atoi for my own library. However, for the test number provided, it isn't doing what it is supposed to do. (I used GCC/MingW32 as the compiler with C'99 enabled. Oh, and there is no overflow-checking in this code!) The code follows: #include <ctype.h>
1
2021
by: ifmusic | last post by:
I have This Code. Token.c: typedef struct { unsigned char orden; char ciudad; unsigned char ip; //que son del router Asociado a la ciudad unsigned int puerto; // """ //int socket; }Ciudad;
2
2772
by: Raf256 | last post by:
Hello, my custom streambuf works fine with output via << and with input via .get() but fails to input via >> or getline... any idea why? -------------------- A custom stream buffer (for encryption, currently it is doing "nothing")
3
1618
by: Chevron Boyde | last post by:
Hi There I have some codes that represent Sale Types i.e. A = On Account, C = Cash, D = Debtor, V = Voucher I want to create an enum or struct to work with the logical names like "Cash" as opposed to "C" I see the enums cannot work with string values like public enum SaleType
3
28617
by: gggram2000 | last post by:
Hi, I need some assistance. I'ved been searching the web for days now and can't find an example that really helps me. I have an EPSON TM-T88IV printer through which I want to open a cash drawer. I have this class in my project: using System; using System.IO; using System.Runtime.InteropServices; namespace PrintDataGrid /* For a standard cash drawer, there's a physical cable that runs from the cash drawer and plugs into the receipt...
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10432
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7141
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.