Article View: comp.lang.c
Article #1107Re: .5 against 0.5f
From: David Brown
Date: Thu, 29 Oct 2026 10:01
Date: Thu, 29 Oct 2026 10:01
72 lines
3953 bytes
3953 bytes
On 28/10/2026 23:33, bartc wrote:
> 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,
Generally, yes. (Though for this code, the compiler would pre-calculate
the results at compile time.) My calculations need to be as precise as
required for their use - but anything beyond that is just a waste. And
if the microcontroller has hardware single-precision floating point but
no double-precision, that wasted runtime is massive.
Technical Details
Message-ID:
<11bv5ij$37mbb$2@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> <11btt7k$2rple$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: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: .5 against 0.5f Date: Thu, 29 Oct 2026 11:01:55 +0100 Organization: A noiseless patient Spider Lines: 72 Message-ID: <11bv5ij$37mbb$2@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> <11btt7k$2rple$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 10:01:56 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3397995"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZTf7ZzWOyRPzaN1Jm9xStvHMkORHmuRA="; posting-host="ee186c62f9168531a82b8aa89ffdfee0" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:dSh0S1ApFjf1NStgnaYvdejsWvY= sha256:YI5pGJs0FCxPKDkLeTyaZneN8qf5cC0RcktxraSLzLI= sha1:A4VOw9RuU5BRe4KRip7nOkIIhIE= sha256:wwGhH8M4ZFqo+O8zaOZFdLoys9I3+syDwxyP2aTJU90= In-Reply-To: <11btt7k$2rple$1@dont-email.me> Content-Language: en-GB, nb-NO