Article View: comp.lang.c
Article #1114Re: .5 against 0.5f
From: David Brown
Date: Thu, 29 Oct 2026 12:55
Date: Thu, 29 Oct 2026 12:55
33 lines
2844 bytes
2844 bytes
On 29/10/2026 13:01, Keith Thompson wrote: > makendo <makendo@makendo.invalid> writes: >>> I ordered a couple of blu-rays from amazon last week, and the >>> confirmation email had the following for the total price: >>> Total >>> 75.71000000000001 USD >>> Appears they may be using FP instead of integer fixed point >>> arithmetic >>> for pricing. >> >> And this is why you should use the lowest value denominator when >> storing monetary values -- store USD values in cents, CNY values >> in fen, EUR values in cents, etc... > [...] > > That's not good enough. Discounts, sales taxes, and compound > interest can easily result in amounts that are not multiples of > one cent, which must then be rounded or truncated to the nearest > cent following whatever financial rules are in effect. > What is "good enough" depends on the context. For a lot of situations, floating point, or rounding to the nearest dollar (or thousand dollars, or pounds, or whatever) can be fine. You can happily use values in cents for your shop prices. But for accurate financial records used in accounting, tax records, banking, etc., you are correct that it is not that simple. Countries and groups of countries all have their standards that you need to conform to. Typically the way to get correct values is to keep everything as large integer types of scaled currency denominations - like a thousandth of a cent.
Technical Details
Message-ID:
<11bvfor$3bl7q$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11bpvs7$1e6kc$2@dont-email.me> <11bqnvh$1np7s$1@dont-email.me> <Ue6ES.2265$y_V1.1859@fx12.iad> <11bv5o7$37qph$1@dont-email.me> <11bvcii$3a06t$3@kst.eternal-september.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: .5 against 0.5f Date: Thu, 29 Oct 2026 13:55:54 +0100 Organization: A noiseless patient Spider Lines: 33 Message-ID: <11bvfor$3bl7q$1@dont-email.me> References: <11bpvs7$1e6kc$2@dont-email.me> <11bqnvh$1np7s$1@dont-email.me> <Ue6ES.2265$y_V1.1859@fx12.iad> <11bv5o7$37qph$1@dont-email.me> <11bvcii$3a06t$3@kst.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 29 Oct 2026 12:55:56 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3527930"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18u0TvEly99TqA+iDUNsbhSwDwN8Wsi2os="; posting-host="ee186c62f9168531a82b8aa89ffdfee0" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:+b9WA85JPwEVkbjZokixwGqpwBY= sha256:II/JpK69uHXZLQ3MkW2Bg44VUnMQMC7K61vRstUcGME= sha1:lsCGZ9jYzbZH5y1Ouhqm6wu3Dqc= sha256:wIRYIrecPt2m7clPWtHpu+A07G+CxPRePG1Bv0d3cAY= In-Reply-To: <11bvcii$3a06t$3@kst.eternal-september.org> Content-Language: en-GB, nb-NO