Article View: comp.lang.c
Article #1343Re: .5 against 0.5f
From: Waldek Hebisch
Date: Thu, 05 Nov 2026 22:07
Date: Thu, 05 Nov 2026 22:07
50 lines
3366 bytes
3366 bytes
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> antispam@fricas.org (Waldek Hebisch) writes:
>
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>
>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>>> On 28/10/2026 21:25, Michael S wrote:
>>>
>>> [...]
>>>
>>>>> I'd still likely prefer to write it as (float)0.1 instead of 0.1f.
>>>>> It somewhat depends on my mood in specific day, but likelihood is
>>>>> certainly not in favor of 0.1f.
>>>>
>>>> That seems a bit excessive to me, but okay. The principle is the
>>>> same, making it clear to the reader and the compiler that no
>>>> double-precision arithmetic is involved.
>>>
>>> Except that, at least in the abstract machine, `(float)0.1` produces
>>> a value of type double and converts it to float. It's likely that
>>> a compiler would generate the same code as for `0.1f`, but I'd want
>>> to confirm that if it mattered. (Actually, I'd just write `0.1f`.)
>>
>> Maybe the code will be the same. But in general going via doubles
>> produces less accurate result, [...]
>
> What makes you think using a double will produce a less accurate
> result?
Going via doubles number in the C source will be rounded twice,
first time to a double, then the result of first runding will
be rounded second time. Each rounding step introduces some
error. Resonable heuristic says that errors may be close to
maximal possible and may add. So error may be bigger than
from single rounding.
If you do not want to belive in the heuristic above, consider
IEEE arthmetic and two consequtive single precisin numbers
a and b. Next, take double c in the middle of the interval
between a nad b. According to IEEE "round to even" rule this
double will be rounded to one of the singles above, say a. Take
source number d that rounds to c, but is on the side corresponding
to b. Going via double will produce a as the result. Directly
rounding to single will produce b. It is clear that b is
closer to d than a, so double rounding in this case indeed
produces more error, that is less accuracy.
--
Waldek Hebisch
Technical Details
Message-ID:
<11ciumc$2kigf$1@paganini.bofh.team>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mailReferences:
<11bpvs7$1e6kc$2@dont-email.me> <11bqnvh$1np7s$1@dont-email.me> <11br5td$1to7l$1@dont-email.me> <11br792$1u7e5$1@dont-email.me> <11brgqf$21170$2@dont-email.me> <11bs8rb$26dq0$1@dont-email.me> <20261028222504.00004b8b@yahoo.com> <11bv4q0$37eth$1@dont-email.me> <11bvcea$3a06t$2@kst.eternal-september.org> <11c4mab$jkma$1@paganini.bofh.team> <86eccznycg.fsf@linuxsc.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: .5 against 0.5f
Date: Thu, 5 Nov 2026 22:07:10 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11ciumc$2kigf$1@paganini.bofh.team>
References: <11bpvs7$1e6kc$2@dont-email.me> <11bqnvh$1np7s$1@dont-email.me> <11br5td$1to7l$1@dont-email.me> <11br792$1u7e5$1@dont-email.me> <11brgqf$21170$2@dont-email.me> <11bs8rb$26dq0$1@dont-email.me> <20261028222504.00004b8b@yahoo.com> <11bv4q0$37eth$1@dont-email.me> <11bvcea$3a06t$2@kst.eternal-september.org> <11c4mab$jkma$1@paganini.bofh.team> <86eccznycg.fsf@linuxsc.com>
Injection-Date: Thu, 5 Nov 2026 22:07:10 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="2771471"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2