🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #986

Re: .5 against 0.5f

#986
From: Keith Thompson
Date: Wed, 28 Oct 2026 23:05
56 lines
3728 bytes
Michael S <already5chosen@yahoo.com> writes:
> 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:
[...]
>> > 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.

Agreed -- and I asked him for examples.  (And no, didn't mean to
suggest that there are no such examples.)

And now that I've thought about it a bit more, here's a possible
example:

    float f = rand(); // prevent optimization
    f *= 0.1;
//  f *= 0.1f;

With 0.1, f is promoted to double before being multiplied by 0.1, and
the result is converted to float.  With 0.1f, double is not involved.
And in the generated x86_64 assembly from gcc, I see cvtss2sd and
cvtsd2ss when I use 0.1 that don't appear when I use 0.1f.

Both expressions *might* yield the same result (and do in the small
handful of cases I've checked), but it would take more analysis
than I'm willing to perform to prove it in all cases.

I was too quick to assume that if a compiler sees 0.1 in an expression
involving float operands, it can safely treat it as 0.1f.  Since 0.1
causes the usual arithmetic conversions to convert the other operand
from float to double, I was very probably wrong.

I can still imagine a compiler optimizing `f *= 0.1;` to use
float arithmetic.  I wonder if there are more clear-cut examples.

I'm assuming that FLT_EVAL_METHOD==0, meaning that float expressions
are evaluated as float.  If FLT_EVAL_METHOD==1, float expressions
are evaluated to the range and precision of type double.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Technical Details
Message-ID: <11btv32$2rgo2$1@kst.eternal-september.org>
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!kst.eternal-september.org!.POSTED!not-for-mail
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>

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!kst.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: .5 against 0.5f
Date: Wed, 28 Oct 2026 16:05:06 -0700
Organization: None to speak of
Lines: 56
Message-ID: <11btv32$2rgo2$1@kst.eternal-september.org>
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
Injection-Date: Wed, 28 Oct 2026 23:05:06 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="2999042"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19fmDWsssCfr21Et1nvXv6G"; posting-host="ac4e6fa4e505b2147f0d092402a060c4"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:tYdfsLjrseiezLRCt1pLI5PfhA4= sha256:D5mE0QRmJSz5JsMUuVXQSWlIIkLn4eckJqY8AAL7jmc=
	sha1:oFB6aHFPwsR4yQV9Q4XoQ9+RKPA= sha256:RDMXC1ud7wu9XBcMKCR+pEsZbNrDM5lvIj8xKm1Se6A=