Article View: comp.lang.c
Article #888Re: .5 against 0.5f
From: bartc
Date: Tue, 27 Oct 2026 21:03
Date: Tue, 27 Oct 2026 21:03
51 lines
3321 bytes
3321 bytes
On 27/10/2026 20:05, Kaz Kylheku wrote: > On 2026-10-27, Scott Lurndal <scott@slp53.sl.home> wrote: >> 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. > > That reminds of a joke I have in the TXR Lisp fortune module, > intended for displaying a random quip at REPL startup. > > Oh, look, I got it after just a couple of tries: > > This is the TXR Lisp interactive listener of TXR 302. > Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet. > 1> (quip) > "Coding around Unicode grapheme clusters is a character-building exercise." > 2> (quip) > "defmicro: error: expansion is larger than original form: switch to defmacro." > 3> (prinl (quip)) > "This could be the year of the TXR desktop; I can feel it!" > "This could be the year of the TXR desktop; I can feel it!" > 4> (put-line (quip)) > TXR kind of supports IEEE 754.00000000000003 floating-point. > t > > :) > > FP is fine for pricing: look at how ridiculously close to 75.71 > the above approximation is. You just have to make sure never to > let enough error accumulate so that an intermediate value > is suddenly closer to the wrong penny value than the right one. > (Actually you don't even want to get anywhere near that level > of error.) I've used floats for pricing, I think even 32-bit ones. It should work fine within certain levels of magnitude - you want a few spare bits of precision beyond individual cents. (To clarify, 32 bits may have been used to store prices in a stock file for example, so typical magnitudes would have been small, but calculations and totals would have used 64 bits.) But apart from doing the rounding right as you suggest, the display should be only showing 2 decimals when displaying dollars, not 14. That example is very sloppy.
Technical Details
Message-ID:
<11br3ih$1sscp$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> <20261027125914.162@kylheku.com>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: Tue, 27 Oct 2026 21:03:13 +0000 Organization: A noiseless patient Spider Lines: 51 Message-ID: <11br3ih$1sscp$1@dont-email.me> References: <11bpvs7$1e6kc$2@dont-email.me> <11bqnvh$1np7s$1@dont-email.me> <Ue6ES.2265$y_V1.1859@fx12.iad> <20261027125914.162@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 27 Oct 2026 21:03:13 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1995161"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+KVPGGQHSC78WXIEYPuRf3"; posting-host="4958b3de6f7c2c0fb660485d9fa8802a" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:hqwj/Uox+45P1V7d20bBT8tX8tM= sha256:7xTym9E8F+815Q7dirXmJxHwLVMg8cVKxMKRZcMLmks= sha1:ZPJUAbwKSJMo7ehcElLcxkHgxtg= sha256:2PQ1ClW1/b6lX3Xegt5gb+k/MceFXjnXgODAfr2a9aE= In-Reply-To: <20261027125914.162@kylheku.com> Content-Language: en-GB