473,803 Members | 3,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoiding float and double in monetary calculation

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

Nov 13 '05
14 9822

"Rouben Rostamian" <ro****@pc18.ma th.umbc.edu> wrote in message

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.

This leads to the serious point that you could always have hyper-inflation,
which means that a program handling monetary values is likely to break
unless it uses arbitrary-precision integers.
Nov 13 '05 #11
Keith Thompson <ks*@cts.com> wrote in message
news:ye******** *****@king.cts. com...
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).


ITYM " ... through -$1.27" Some implementations allow -128
in a signed char, but it's not required.

-Mike

Nov 13 '05 #12
Mike Wahler wrote:
Keith Thompson <ks*@cts.com> wrote in message
news:ye******** *****@king.cts. com...
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).


ITYM " ... through -$1.27" Some implementations allow -128
in a signed char, but it's not required.


I guess that means that I'll just have to be more careful about
the checks I write (or start using my thumbs in personal
financial calculations [thus far I've reserved my thumbs as carry
and overflow bits] - I've considered using one thumb as sign and
the other as a ninth [high order] magnitude bit; but I'm not
certain that I actually have a need for the additional capacity -
and I have serious misgivings about undetected under-/overflow
conditions...
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Nov 13 '05 #13
John L Fjellstad wrote:
Morris Dovey wrote:
Question for you: What is the 1/100 part of a Euro called?


Cent.


Thanks. Should be easy (even for me) to remember.
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Nov 13 '05 #14
On Wed, 20 Aug 2003 22:08:40 -0400, in comp.lang.c , wane
<no****@intechs .net> wrote:
Hello,
I have heard that one should avoid using float and double in monetary
calculation because of the lack of preciseness.
This is certainly a viewpoint. For what its worth, I work in
investment banking, and we find doubles quite adequate even for large
scale financial transactions. In over a decade in the buisness I have
come across only a couple of instances where it actually mattered.
What is a good alternative?


If you want perfect precision, use integer types, and a scaling factor
to turn back into pennies. Note that this can limit your range unless
you use special big number handling routines. The assets of many a
company, converted into say Turkish Lira, would overflow most builtin
types. It may also be slower, esp if you have to use a special
library.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #15

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

Similar topics

11
508
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";
13
2726
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
17
11350
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?
2
1408
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...
60
7236
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
27
2879
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.
116
36007
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;
13
6193
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
2
5352
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...
0
9700
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
9564
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,...
0
10310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10292
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,...
0
10068
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
6841
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
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.