Hello,
I have heard that one should avoid using float and double in monetary
calculation because of the lack of preciseness.
What is a good alternative?
Thanks 14 9819
wane <no****@intechs .net> wrote in message
news:bi******** ****@ID-18576.news.uni-berlin.de... Hello, I have heard that one should avoid using float and double in monetary calculation because of the lack of preciseness. What is a good alternative?
Use an integer type, e.g. 'long'. E.g. if representing
U.S. currency, store pennies, not dollars, and do decimal point
detection and placement during i/o.
-Mike
>calculation because of the lack of preciseness.
This depends, of course, on how much precision you need. If you're
estimating the cost of 1500 government toilet seats, "about 5 billion
dollars" might be good enough. If you're auditing the cost of 1500
toilet seats, you may need to worry about hundredths of cents. What is a good alternative?
Integer quantities of the smallest monetary unit you need to deal
with (e.g. cents). If you've got an integer type bigger than 32
bits, try to use it. C89 doesn't guarantee that there is such a
type.
Gordon L. Burditt
wane writes: I have heard that one should avoid using float and double in monetary calculation because of the lack of preciseness. What is a good alternative?
Some compilers offer a BCD (binary coded decimal) data type via an included
library.
In article <bi************ @ID-18576.news.uni-berlin.de>,
wane <no****@intechs .net> wrote: I have heard that one should avoid using float and double in monetary calculation because of the lack of preciseness.
Bill Gates has 100,000,000,000 in his savings account. His bank's
statement is printed using:
#include <stdio.h>
int main(void)
{
float y = 1e11F;
printf("Your balance is %.2f\n", y);
return 0;
}
The bank's computer prints the result as:
Your balance is 99999997952.00
Then Bill buys the bank...
What is a good alternative?
When dealing with Bill Gates' bank account, use an arbitrary precision
arithmetic library. Search the web for GNU MP library, but don't
tell Bill it's GNU.
--
Rouben Rostamian <ro*******@umbc .edu>
wane wrote: I have heard that one should avoid using float and double in monetary calculation because of the lack of preciseness.
That's *not* true.
Double precision floating-point numbers are very precise.
Monetary calculations aren't very precise.
It's just that special rules for rounding are used
in calculations involving money.
If, for example, you divide $1.00 among three people,
there will be an extra penny left over.
Banks always keep this extra penny ;-)
wane wrote: I have heard that one should avoid using float and double in monetary calculation because of the lack of preciseness.
It depends on the range of values possible and the ability of the
programmers. The oldest stock exchange in the USA (PHLX) performs
monetary calculations using doubles for equity, option, and
currency trading - without precision problems.
What is a good alternative?
Again, it depends on the range of values. For a program on my
machine to keep track of my checking account, a char should be
sufficient to handle the full range in pennies ($1.27 through
-$1.28).
Your checking account may need to use long int; and your central
bank would probably need long long int values.
Question for you: What is the 1/100 part of a Euro called?
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
In article <ne************ ********@tomato .pcug.org.au>, kevin@-nospam-
pcug.org.au says... Gordon Burditt <go***********@ sneaky.lerctr.o rg> wrote:calculation because of the lack of preciseness.
This depends, of course, on how much precision you need. If you're estimating the cost of 1500 government toilet seats, "about 5 billion dollars" might be good enough. If you're auditing the cost of 1500 toilet seats, you may need to worry about hundredths of cents.
At a bit over 3 million dollars a pop, those had better be solid gold toilet seats.
They're $3 toilet seats. Come on, how do you think the NSA funds all
their super secret underground computers? $3million buck toilet seats.
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in
news:3F******** ******@jpl.nasa .gov: wane wrote:
I have heard that one should avoid using float and double in monetary calculation because of the lack of preciseness.
That's *not* true. Double precision floating-point numbers are very precise. Monetary calculations aren't very precise. It's just that special rules for rounding are used in calculations involving money.
If, for example, you divide $1.00 among three people, there will be an extra penny left over. Banks always keep this extra penny ;-)
I thought it was redirected to the bank account of the
programmer that wrote the code. That's how it used to
work in the good old days (*).
Phil T
(*) This is an urban legend.
Morris Dovey <mr*****@iedu.c om> writes:
[...] Again, it depends on the range of values. For a program on my machine to keep track of my checking account, a char should be sufficient to handle the full range in pennies ($1.27 through -$1.28).
You should either explicitly use signed char, or be more careful about
bouncing checks.
--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Gurikar |
last post by:
Hello,
Can any one tell me is the code below correct.
#include<iostream.h>
int main()
{
int i = 1;
float f = i / 2;
if(f)
cout<<"HI";
|
by: Michele Guidolin |
last post by:
Hello to everybody.
I'm doing some benchmark about a red black Gauss Seidel algorithm with 2
dimensional grid of different size and type, I have some strange result
when I change the computation from double to float.
Here are the time of test with different grid SIZE and type:
SIZE 128 256 512
|
by: kiplring |
last post by:
float sum = (float)Math.Sqrt( floatA*floatA + floatB*floatB);
I'm using DirectX with c#. But the Math class in .net framework has a
problem. It is "double" base!
So I'm doing type casting every line with Math class.
Is there any Math library float based?
|
by: julien |
last post by:
Hi, I am using Sybase 12.5 dataserver and ASP.NET
I am calling a stored procedure from my asp.net page, in this stored
procedure, I have 2 float fields that are returned. One is directly
taken from one float column of a table, and the other one is a
substraction of 2 float columns of a table. The first field is all the
time ok, whereas the calculated field is almost all the time wrong,
that is it returns things like :
1,332294 - 1,334709...
|
by: Erick-> |
last post by:
hi all...
I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best? when should we
use float or double??
thanks
Erick
| |
by: galt_57 |
last post by:
I need to do just a few multiplies of long integers and have a divide
by ten. Can I avoid using floats? Can I use two longs as a 64 bit value
somehow? Thanks.
|
by: Dilip |
last post by:
Recently in our code, I ran into a situation where were stuffing a
float inside a double. The precision was extended automatically
because of that. To make a long story short, this caused problems
elsewhere in another part of the system where that figure was used for
some calculation and some eventual truncation led to the system going
haywire. So my question is, given this code:
int main()
{
float f = 59.89F;
|
by: Shirsoft |
last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding
error in the 8th digit. Unfortunately because of the algorithm we use,
the errors percolate into higher digits.
C++ code is
------------------
b += (float)(mode *val);
On 32 bit(intel , vs 2003, C++), some watch variables are
|
by: Mike |
last post by:
I'm running DB2 v7 for z/OS. When I use SPUFI, SELECT CAST(6.0 AS
FLOAT)/CAST(10.0 AS FLOAT) FROM SYSIBM.SYSDUMMY1 returns
0.6000000000000000E+00. When I use DSNTIAUL,DSNTEP2, or DSNALI (call
attach facility), the same statement returns 0.59999999999999999E 00.
The only reason I$B!G(Bve heard to explain this behavior is that float
stores too much precision, but I$B!G(Bve used double-precision floating-
point data types in SQL...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |