Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Toy code for currency handling Date: Wed, 29 Nov 2017 09:25:40 -0800 Organization: None to speak of Lines: 34 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="fd1e1c2ac75c01ecf8d3296b6b1ba920"; logging-data="9287"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/IeSU4dWI1gqqky8C1/8I3" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:c8Dgi00cn15+qM1YKopOO8dMLBI= sha1:3oib+qmSGpItaLp3sw3Y56AsUrs= Xref: csiph.com comp.lang.c:123610 Noob writes: > On 29/11/2017 11:50, Noob wrote: > >> 2**64 = 1.8e19 >> Using one millionth dollars as the base unit, we can express >> values from 0 to 18000 billion dollars. >> >> But I guess this doesn't solve rounding issues, which have to >> be dealt with when multiplying. >> >> I'll write a toy program to experiment with that :-) > > I played around with a different approach: storing the integral part > in a u64, and the fractional part (in billionth) in a u32 (effectively > ~94 significant bits). I'm still not quite sure how to handle rounding. [...] As I understand it, there are specific rules, in laws and/or regulations, that govern exactly how rounding is to be performed. I have no idea what those rules are (I've never particularly needed to know). If I were going to work on production software that deals with money, I'd have to learn them. 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. -- 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"