🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1160

Re: .5 against 0.5f

#1160
From: Michael S
Date: Fri, 30 Oct 2026 09:58
74 lines
4525 bytes
On Wed, 28 Oct 2026 16:05:06 -0700
Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:

> 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.
> 

Since we're talking about single precision, analysis is not necessary.
Any hypothesis can be easily tested by exhausting.
In case of (float)(f * 0.1) == f * 0.1f, it fails for almost exactly one
fifth of the numbers, including 1.00000011920928955f - the next float
after 1.0f.


> 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 don't know what exactly is allowed by the Standard (that is, I know
what's written, I don't know how peoples with lowyers attitude can
interpret it), but will be extremely surprised if any "major" compiler
is doing such optimization on IEEE-754 compliant platform in absence of
non-compliance flags like fast-math.

> 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.
> 

Technical Details
Message-ID: <20261030115805.0000644a@yahoo.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-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> <11btv32$2rgo2$1@kst.eternal-september.org>

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: Michael S <already5chosen@yahoo.com>
Newsgroups: comp.lang.c
Subject: Re: .5 against 0.5f
Date: Fri, 30 Oct 2026 11:58:05 +0200
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <20261030115805.0000644a@yahoo.com>
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>
	<11btv32$2rgo2$1@kst.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 30 Oct 2026 09:58:12 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="108050"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+PCnbdVzsdUrYMYspPCntxEDR+9IVcrU0="; posting-host="9bad7b7abbe8f8f1a5542e1fed1b4eb7"
Cancel-Lock: sha1:U0n+LpD32Ma3u0YzVgkR8K9LeVA= sha256:1nSqydpfKwY5R5VF6PmWWHnj0IUHpteNDos6QKRWOI8=
	sha1:1I+UF1diOP96TLxRY4EUowl73Po= sha256:sWRbgtK3YS7lnCBr+eDjz7FRWo9xmMHjxEkrBm8xSak=
X-Newsreader: Claws Mail 4.1.1 (GTK 3.24.34; x86_64-w64-mingw32)