Article View: comp.lang.c
Article #984Re: .5 against 0.5f
From: bartc
Date: Wed, 28 Oct 2026 22:33
Date: Wed, 28 Oct 2026 22:33
63 lines
3374 bytes
3374 bytes
On 28/10/2026 22:12, Michael S wrote:
> On Wed, 28 Oct 2026 14:26:05 -0700
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 28/10/2026 01:49, Lawrence D’Oliveiro wrote:
>>>> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote:
>>>>> However, if you need double precision, you shouldn't be using the
>>>>> "f" suffix.
>>>> Is there any situation where you *should* use the “f” suffix?
>>>> I am thinking: no.
>>>
>>> Try thinking harder.
>>>
>>> I use the "f" suffix all the time in my code, because I do not want
>>> double precision. In many situations, single precision arithmetic
>>> is at least twice as fast as double precision - in some situations
>>> it can be two orders of magnitude faster.
>>
>> If you define objects as type float rather than double, do you have
>> examples of code where using 0.1f rather than 0.1 makes a difference?
>>
>> One simple example: These two declarations result in identical code
>> using gcc:
>>
>> float f = 0.1f;
>> float f = 0.1;
>>
>
> David almost certainly was talking about use of 0.1f in arithmetic
> expressions rather than in simple initialization.
>
>
Yeah, intermediate calculations using 64 bits can give a different and
more precise result even after it has been converted to 32 bits.
For example this:
#include <stdio.h>
int main(void) {
float a = 0.1f * 0.1f;
float b = 0.1 * 0.1;
printf("%20.20f\n", a);
printf("%20.20f\n", b);
printf("%20.20f\n", 1 - a * 100);
printf("%20.20f\n", 1 - b * 100);
}
Displays:
0.01000000070780515671
0.00999999977648258209
-0.00000011920928955078
0.00000000000000000000
on gcc 9.x on x64.
It will need the compiler to do the reduction, but DB is likely using a
cross-compiler so it is not being done within target device,
Technical Details
Message-ID:
<11btt7k$2rple$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> <11br5td$1to7l$1@dont-email.me> <11br792$1u7e5$1@dont-email.me> <11brgqf$21170$2@dont-email.me> <11bs8rb$26dq0$1@dont-email.me> <11btp9d$2ojn1$4@kst.eternal-september.org> <20261029001256.00001179@yahoo.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: Wed, 28 Oct 2026 22:33:23 +0000 Organization: A noiseless patient Spider Lines: 63 Message-ID: <11btt7k$2rple$1@dont-email.me> 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> <11btp9d$2ojn1$4@kst.eternal-september.org> <20261029001256.00001179@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 28 Oct 2026 22:33:25 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3008174"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180WEi2h3JDPV3u9NxRJgLa"; posting-host="97cf5f3d51e5ee620ae24cc334b2261a" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:tEUB5CFZsHtKy8dj5+ySE291Lr4= sha256:XfPujDJAz3T/Opvxx/DyM9xHsNRAaW7L/TO2OEvXsl0= sha1:mPcKy0g8d9QpZYHkeN9f6Xe09jo= sha256:xQSOHbMgpAd+tDL24Ca6/1E8+qEJmT3UXZ8liGfbVUI= In-Reply-To: <20261029001256.00001179@yahoo.com> Content-Language: en-GB