Article View: comp.lang.c
Article #875Re: .5 against 0.5f
From: Kaz Kylheku
Date: Tue, 27 Oct 2026 20:05
Date: Tue, 27 Oct 2026 20:05
47 lines
2934 bytes
2934 bytes
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.) If all your system does is take a value from a ledger, do a few simple calcluations on it, then round it to the best approximation of the nearest penny before recording back into the ledger, you are fine. You then just have to be careful about converting the values to digits. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca
Technical Details
Message-ID:
<20261027125914.162@kylheku.com>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>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: Kaz Kylheku <046-301-5902@kylheku.com> Newsgroups: comp.lang.c Subject: Re: .5 against 0.5f Date: Tue, 27 Oct 2026 20:05:44 -0000 (UTC) Organization: A noiseless patient Spider Lines: 47 Message-ID: <20261027125914.162@kylheku.com> References: <11bpvs7$1e6kc$2@dont-email.me> <11bqnvh$1np7s$1@dont-email.me> <Ue6ES.2265$y_V1.1859@fx12.iad> Injection-Date: Tue, 27 Oct 2026 20:05:44 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1942009"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX192ks0yGmw6lWHzwKj+EwXaot6wtzS1udg="; posting-host="affe2e68f188d099451ee49d810ad8d6" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:d9lHz4lnM45sOViruUOWYkXFX80= sha256:lj+jcy/lOl0yVfXRw3ceXJdaH8R1cfAHd4RAKOs7tSY= sha1:Fjnf/+vqB7C9/DSuUKqi5x0x0Qk= sha256:VOCnE0fVlQnQ82sUr32IWHXL7DanctD2UEVZpo4HAzU=