Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #123620

Re: Toy code for currency handling

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Toy code for currency handling
Date 2017-11-29 11:31 -0800
Organization None to speak of
Message-ID <lnmv34yj9l.fsf@kst-u.example.com> (permalink)
References (5 earlier) <ovmirc$cs7$1@dont-email.me> <lnvahtxai3.fsf@kst-u.example.com> <ovmuqj$cgs$1@dont-email.me> <lnr2sgykjb.fsf@kst-u.example.com> <sGDTB.97528$3X1.57428@fx28.am4>

Show all headers | View raw


bartc <bc@freeuk.com> writes:
> On 29/11/2017 19:03, Keith Thompson wrote:
>> Noob <root@127.0.0.1> writes:
>>> On 29/11/2017 18:25, Keith Thompson wrote:
>>>> If you're writing something for your own use, say to balance your
>>>> checkbook, and a 1-cent error on an interest calculation isn't fatal,
>>>> you probably don't need to worry about it.
>>>
>>> I would definitely *not* need 94 digits to balance my checkbook ;-)
>> 
>> If you have an interest-bearing account, and an interest payment
>> increases your balance by an amount very very close to 0.5 cents,
>> you might need some large number of digits to determine which way
>> to round.
>
> That might not help. Even if you have a million digits, one machine 
> might result in 0.4999...9999, and another 0.5000...0001, and they will 
> round in different ways to get 0.0 and 1.0 cents.
>
> A strategy needs to be used that will always yield the same result.
>
> Using integers won't help either (for working out interest), without 
> specifying exactly how such calculations are to be done.

Here's an example of the rules for converting between pre-Euro
nationatal currencies and Euros:

    ARTICLE 4; The conversion rates shall be adopted as one euro
    expressed in terms of each of the national currencies. They
    shall be adopted with six significant figures. The conversion
    rates themselves must not be rounded or truncated when
    making conversions. The conversion rates shall be used for
    conversions either way between the euro and the national
    currency units. Inverse rates derived from the conversion
    rates shall not be used. Monetary amounts to be converted
    from one national currency unit into another shall first be
    converted into a monetary amount expressed in the euro unit,
    which amount may be rounded to not less than three decimal
    places and shall then be converted into the other national
    currency unit. No alternative method of calculation may be used,
    unless it produces the same result.

    ARTICLE 5; Monetary amounts to be paid or accounted for,
    when a rounding takes place after a conversion into the euro
    unit according to article 4, shall be rounded up or down to the
    nearest cent, Monetary amounts to be paid or accounted for which
    are converted into a national currency unit shall be rounded up
    or down to the nearest sub-unit, or in the absence of a sub-unit
    to the nearest unit or according to national law or practice to
    a multiple or fraction of the sub-unit or unit of the national
    currency unit. If the application of the conversion rates gives
    a result which is exactly half-way, the sum shall be rounded up.

http://www.sysmod.com/eurofaq.htm#ROUNDING

This all appears to be specified in terms of decimal representations.
The phrase "not less than three decimal places" is suprisingly
ambiguous; I can imagine cases where the decision to use, say,
4 vs. 5 decimal places might yield results that differ by 1 cent.

There are, as I understand it, a *lot* of written rules like this
in various contexts.  I think consistency of results is generally
considered more important than mathematical perfection.  You might
need 94 or more digits, or even arbitrarily many digits, to compute
a correctly rounded interest payment, but there are bound to be
financial regulations in place that specify the rounding without
requiring ludicrous precision.  And you might get into some trouble
if you do a computation using more precision than the regulations
specify, if you end up with a different result.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
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"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

[computerphile] Why can't floating point do money? luser droog <luser.droog@gmail.com> - 2017-11-22 09:39 -0800
  Re: [computerphile] Why can't floating point do money? "F. Russell" <fr@random.info> - 2017-11-23 13:11 +0000
    Re: [computerphile] Why can't floating point do money? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-23 05:35 -0800
    Re: [computerphile] Why can't floating point do money? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-23 13:47 +0000
    Re: [computerphile] Why can't floating point do money? "F. Russell" <fr@random.info> - 2017-11-23 15:20 +0000
    Re: [computerphile] Why can't floating point do money? Robert Wessel <robertwessel2@yahoo.com> - 2017-11-24 00:53 -0600
      Re: [computerphile] Why can't floating point do money? supercat@casperkitty.com - 2017-11-24 14:16 -0800
        Re: [computerphile] Why can't floating point do money? Robert Wessel <robertwessel2@yahoo.com> - 2017-11-24 23:50 -0600
    Re: [computerphile] Why can't floating point do money? Noob <root@127.0.0.1> - 2017-11-24 09:44 +0100
      Re: [computerphile] Why can't floating point do money? Jerry Stuckle <jstucklex@attglobal.net> - 2017-11-24 09:18 -0500
        Re: [computerphile] Why can't floating point do money? Noob <root@127.0.0.1> - 2017-11-29 11:50 +0100
          Re: Toy code for currency handling Noob <root@127.0.0.1> - 2017-11-29 16:13 +0100
            Re: Toy code for currency handling Noob <root@127.0.0.1> - 2017-11-29 16:53 +0100
            Re: Toy code for currency handling Robert Wessel <robertwessel2@yahoo.com> - 2017-11-29 10:42 -0600
            Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-11-29 09:25 -0800
              Re: Toy code for currency handling Noob <root@127.0.0.1> - 2017-11-29 19:38 +0100
                Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-11-29 11:03 -0800
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-11-29 19:14 +0000
                Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-11-29 11:31 -0800
                Re: Toy code for currency handling Robert Wessel <robertwessel2@yahoo.com> - 2017-12-01 00:04 -0600
                Re: Toy code for currency handling fr314159@gmail.com - 2017-12-01 12:41 -0800
                Re: Toy code for currency handling supercat@casperkitty.com - 2017-12-01 15:24 -0800
                Re: Toy code for currency handling Noob <root@127.0.0.1> - 2017-12-03 18:08 +0100
                Re: Toy code for currency handling fr314159@gmail.com - 2017-12-04 10:06 -0800
                Re: Toy code for currency handling jameskuyper@verizon.net - 2017-12-04 10:19 -0800
                Re: Toy code for currency handling fr314159@gmail.com - 2017-12-04 11:35 -0800
                Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-12-04 11:56 -0800
                Re: Toy code for currency handling Jerry Stuckle <jstucklex@attglobal.net> - 2017-12-04 15:01 -0500
                Re: Toy code for currency handling "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-12-05 07:14 +0100
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-05 09:18 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-05 11:31 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-05 14:13 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-05 14:39 +0000
                Re: Toy code for currency handling Melzzzzz <Melzzzzz@zzzzz.com> - 2017-12-05 15:00 +0000
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-05 15:19 +0000
                Re: Toy code for currency handling Melzzzzz <mel@zzzzz.com> - 2017-12-05 16:31 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-05 16:07 +0000
                Re: Toy code for currency handling Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-12-06 01:25 +0000
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 02:00 +0000
                Re: Toy code for currency handling Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-12-06 03:04 +0000
                Re: Toy code for currency handling scott@slp53.sl.home (Scott Lurndal) - 2017-12-05 16:14 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-05 17:28 +0100
                Re: Toy code for currency handling supercat@casperkitty.com - 2017-12-05 11:25 -0800
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-05 21:47 +0000
                Re: Toy code for currency handling supercat@casperkitty.com - 2017-12-05 14:13 -0800
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-05 22:40 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-06 00:38 +0100
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-06 00:21 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 02:22 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-06 10:04 +0100
                Re: Toy code for currency handling supercat@casperkitty.com - 2017-12-06 08:31 -0800
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-05 16:25 +0100
                Re: Toy code for currency handling already5chosen@yahoo.com - 2017-12-05 08:33 -0800
                Re: Toy code for currency handling Melzzzzz <Melzzzzz@zzzzz.com> - 2017-12-05 19:09 +0000
                Re: Toy code for currency handling already5chosen@yahoo.com - 2017-12-05 12:27 -0800
                Re: Toy code for currency handling Melzzzzz <Melzzzzz@zzzzz.com> - 2017-12-05 20:40 +0000
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-05 16:42 +0000
                Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-12-05 09:39 -0800
                Re: Toy code for currency handling scott@slp53.sl.home (Scott Lurndal) - 2017-12-05 17:52 +0000
                Re: Toy code for currency handling Ian Collins <ian-news@hotmail.com> - 2017-12-06 07:59 +1300
                Re: Toy code for currency handling Melzzzzz <Melzzzzz@zzzzz.com> - 2017-12-05 19:12 +0000
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-05 21:59 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-06 00:44 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 01:52 +0000
                Re: Toy code for currency handling Ian Collins <ian-news@hotmail.com> - 2017-12-06 19:40 +1300
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 12:03 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-06 10:30 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 11:40 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-06 14:03 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 14:32 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-06 16:25 +0100
                Re: Toy code for currency handling already5chosen@yahoo.com - 2017-12-06 07:47 -0800
                Re: Toy code for currency handling scott@slp53.sl.home (Scott Lurndal) - 2017-12-06 16:06 +0000
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 18:08 +0000
                Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-12-06 11:22 -0800
                Re: Toy code for currency handling supercat@casperkitty.com - 2017-12-06 09:24 -0800
                Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-12-06 11:23 -0800
                Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-12-06 11:18 -0800
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 19:59 +0000
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 20:02 +0000
                Re: Toy code for currency handling Ian Collins <ian-news@hotmail.com> - 2017-12-07 09:39 +1300
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 21:36 +0000
                Re: Toy code for currency handling Ian Collins <ian-news@hotmail.com> - 2017-12-07 11:15 +1300
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 23:36 +0000
                Re: Toy code for currency handling Ian Collins <ian-news@hotmail.com> - 2017-12-07 12:49 +1300
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-06 23:58 +0000
                Re: Toy code for currency handling Ian Collins <ian-news@hotmail.com> - 2017-12-07 13:08 +1300
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-07 01:51 +0000
                Re: Toy code for currency handling Ian Collins <ian-news@hotmail.com> - 2017-12-07 14:54 +1300
                Re: Toy code for currency handling Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-12-07 11:27 -0500
                Re: Toy code for currency handling Sjouke Burry <burrynulnulfour@ppllaanneett.nnll> - 2017-12-07 03:38 +0100
                Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-12-06 20:39 -0800
                Re: Toy code for currency handling "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-12-07 12:45 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-07 12:21 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-07 14:53 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-07 14:31 +0000
                Re: Toy code for currency handling "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-12-07 15:33 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-07 14:53 +0000
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-07 14:46 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-07 20:28 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-07 20:03 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-07 22:27 +0100
                Re: Toy code for currency handling bartc <bc@freeuk.com> - 2017-12-07 22:45 +0000
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-08 00:39 +0100
                Re: Toy code for currency handling Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-12-07 21:38 +0000
                Re: Toy code for currency handling Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-12-07 15:24 +0000
                Re: Toy code for currency handling supercat@casperkitty.com - 2017-12-07 07:42 -0800
                Re: Toy code for currency handling scott@slp53.sl.home (Scott Lurndal) - 2017-12-07 14:58 +0000
                Re: Toy code for currency handling already5chosen@yahoo.com - 2017-12-06 01:45 -0800
                Re: Toy code for currency handling Jerry Stuckle <jstucklex@attglobal.net> - 2017-12-05 08:38 -0500
                Re: Toy code for currency handling "James R. Kuyper" <jameskuyper@verizon.net> - 2017-12-04 15:16 -0500
                Re: Toy code for currency handling asetofsymbols@gmail.com - 2017-12-05 02:56 -0800
                Re: Toy code for currency handling Keith Thompson <kst-u@mib.org> - 2017-12-04 11:04 -0800
                Re: Toy code for currency handling David Brown <david.brown@hesbynett.no> - 2017-12-05 09:33 +0100
                Re: Toy code for currency handling Jerry Stuckle <jstucklex@attglobal.net> - 2017-12-04 14:56 -0500
                Re: Toy code for currency handling Robert Wessel <robertwessel2@yahoo.com> - 2017-12-04 20:44 -0600
                Re: Toy code for currency handling herrmannsfeldt@gmail.com - 2017-12-06 21:59 -0800
          Re: [computerphile] Why can't floating point do money? Robert Wessel <robertwessel2@yahoo.com> - 2017-11-29 10:51 -0600
          Re: [computerphile] Why can't floating point do money? Jerry Stuckle <jstucklex@attglobal.net> - 2017-11-30 16:18 -0500
            Re: [computerphile] Why can't floating point do money? scott@slp53.sl.home (Scott Lurndal) - 2017-11-30 22:13 +0000
      Re: [computerphile] Why can't floating point do money? Robert Wessel <robertwessel2@yahoo.com> - 2017-11-24 10:19 -0600
        Re: [computerphile] Why can't floating point do money? Spiros Bousbouras <spibou@gmail.com> - 2017-11-24 16:43 +0000
          Re: [computerphile] Why can't floating point do money? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-24 08:53 -0800
          Re: [computerphile] Why can't floating point do money? bert <bert.hutchings@btinternet.com> - 2017-11-24 08:57 -0800
        Re: [computerphile] Why can't floating point do money? bartc <bc@freeuk.com> - 2017-11-24 16:57 +0000
          Re: [computerphile] Why can't floating point do money? Robert Wessel <robertwessel2@yahoo.com> - 2017-11-24 23:35 -0600
            Re: [computerphile] Why can't floating point do money? Melzzzzz <Melzzzzz@zzzzz.com> - 2017-11-25 05:59 +0000
              Re: [computerphile] Why can't floating point do money? supercat@casperkitty.com - 2017-11-27 10:14 -0800
              Re: [computerphile] Why can't floating point do money? Robert Wessel <robertwessel2@yahoo.com> - 2017-11-30 23:56 -0600

csiph-web