Article View: comp.lang.c
Article #1115Re: .5 against 0.5f
From: bartc
Date: Thu, 29 Oct 2026 14:32
Date: Thu, 29 Oct 2026 14:32
51 lines
3495 bytes
3495 bytes
On 29/10/2026 12:55, David Brown wrote: > 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. For /storing/ values in some database, then float binary will be fine, evening using dollars and decimals cents such as $1.99. But there are a couple of provisos: * There is limited precision of some 22 or 23 bits, which caps the magnitude that can be represented * It must be stipulated that the fractional part always represents between 0 and 99 hundredths of a dollar, even if the actual value cannot be exact (so 0.75 can be stored exactly, but not 0.74). There will never be a value halfway between two consecutive cents. With those in place, then any stored value can be converted to an exact number of cents (for example, (int)(x*100+0.5) for positive x), or to some other exact representation. Working directly with such float values is tricky. Oh, there is one more proviso: if printing such a value, you must only show 2 decimal places, not 14! So you don't get stupid output like 0.74000000953674316 USD.
Technical Details
Message-ID:
<11bvldc$3e02t$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> <11bvfor$3bl7q$1@dont-email.me>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: bartc <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: .5 against 0.5f Date: Thu, 29 Oct 2026 14:32:12 +0000 Organization: A noiseless patient Spider Lines: 51 Message-ID: <11bvldc$3e02t$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> <11bvfor$3bl7q$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 29 Oct 2026 14:32:13 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3604573"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DBRjrlhzK1b/UKKw8mX6g"; posting-host="04523fb99fc063320f1c7a365a16a0ee" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:hCCA8Xf8eY/A6imlO07OicfIyoA= sha256:JUlXdTgYYLUH6GGAKxVDyF6h1qGoaaygxTDUiUiUBvc= sha1:ZnkGX1XtL3688/msE0Fex8pAZOg= sha256:s1QFSgzoLCEYRVRA76X0XPOpGJkr0MxsDD4q+AUzAWQ= In-Reply-To: <11bvfor$3bl7q$1@dont-email.me> Content-Language: en-GB