Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Date: Fri, 09 Dec 2016 09:23:59 -0800 Organization: None to speak of Lines: 47 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="2ad4785427d430f26583540ef5e88668"; logging-data="5902"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+j/uAUmBZ7Tz6WcSlv2I+k" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:aAmb4z0jnJLacA0oudfyoWmR26M= sha1:nJFQCIYPetQlrKx2fdmSH9fGeGk= Xref: csiph.com comp.lang.c:98574 John Bode writes: > On Monday, December 5, 2016 at 8:32:38 AM UTC-6, potomac wrote: > > [snippity] > >> float value[5]; > > Apropos of nothing else in this thread, note that a lot of (if not most) > real-world financial software does *NOT* use floating point to store > currency amounts. It's far more common to use an integral type or an > integral sequence (such as BCD) to store currency values, scaled to the > smallest unit (cents or mils). > > For example, the integral value 12345 can represent $123.45 in cents, or > $12.345 in mils. Floating-point introduces a bit too much imprecision, > especially since you cannot represent a value like 0.1 or 0.01 *exactly* > in most floating-point formats. > > We may use floating point for some calculations, but the result will be > stored back to an integral type (with appropriate rounding rules). Agreed (I think). However, storing money values in integers representing the smallest unit (cents, mils, whatever), though it's almost certainly better than floating-point, doesn't entirely solve the problem. There are financial calculations that yield results that are not whole numbers of cents or mils. For example, you might need to start with some amount of money and then apply one day's interest at 5% per annum, compounded daily. Multiplying a whole number by 1.05**(1/365) (or is it 360?) will not yield a whole number. There are, as I understand it, consistent rules, set by law, for how these calculations are to be performed. Doing the computation in a manner that yields a different result than the one required by the rules can get you in big trouble. I have no idea what those rules actually are, but if I were working on financial software I presumably would have to. (It's not inconceivable that the rules are written in terms of floating-point calculations.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"