🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #968

Re: .5 against 0.5f

#968
From: Steven G. Kargl
Date: Wed, 28 Oct 2026 19:10
116 lines
4856 bytes
On Wed, 28 Oct 2026 19:15:45 +0100, fir wrote:

> Steven G. Kargl pisze:
>> On Wed, 28 Oct 2026 07:51:13 +0100, fir wrote:
>> 
>>> (should i also write 0.0f insetad of 0 BTW?)
>> 
>> A lot of context removed.  No.  IMO, if you have
>> a numerical constant that can be written as an
>> integer and that integer is less than 2^P
>> where P is the precision of the floating point
>> type of interest (here, 2^P means 2 raised to
>> the Pth power), then simply write the numerical
>> constant as an integer.  It's easier to read.
>> 
>> float foo(float a) {
>>    float x = 25 * a;
>>    return (x);
>> }
>> 
>> A compiler ought to convert 25 in the above to
>> 25.f as 25 is exactly representable (at least in
>> IEEE-754 binary32 format) in float.
>> 
> there is something like integer*float multiplication , or not?
> if so 25*a and 25.*a or 25.0f*a could make a difference

For 25, 25., and 25.0f, the compiler ought to produce the
same assembly.  Why?  Because the integer 25 is exactly
representable in both float (IEEE-754 binary32) and double
(IEEE-754 binary64), and the float 25.0f is exactly
representable in double.  A compiler can take advantage of
this to produce an optimal assembly listing.  At least with
clang, one sees this.

float
foo(int n, float x)
{
//  float a = x * 25;
//  float a = x * 25.f;
   float a = x * 25.;
   return (a);
}

Selecting each declaration in order yields

% cc -o z.s -S -O3 j8.c && md5 -r z.s
0879b3f8c5924476e64657d4b17a9405 z.s
% cc -o z.s -S -O3 j8.c && md5 -r z.s
0879b3f8c5924476e64657d4b17a9405 z.s
% cc -o z.s -S -O3 j8.c && md5 -r z.s
0879b3f8c5924476e64657d4b17a9405 z.s

Now, take x/10, x*0.1, and x*0.1f.

float
foo(int n, float x)
{
//  float a = x * 0.1;
//  float a = x * 0.1f;
   float a = x / 10;
   return (a);
}

The integer 10 is exactly representable, but a compiler will
likely produce a division, and that's what we see with clang

.LCPI0_0:
        .long   0x41200000                      # float 10
...
        .cfi_def_cfa_register %rbp
        divss   .LCPI0_0(%rip), %xmm0
        popq    %rbp
        .cfi_def_cfa %rsp, 8
        retq

The real number 0.1 is not exactly representable in either 
float or double.  The compiler will chose the best available
floating point number nearest to 0.1 and 0.1f (and this 
depends on the rounding mode).  With 0.1f and clang, you get 

.LCPI0_0:
        .long   0x3dcccccd                      # float 0.100000001
...
        .cfi_def_cfa_register %rbp
        mulss   .LCPI0_0(%rip), %xmm0
        popq    %rbp
        .cfi_def_cfa %rsp, 8
        retq

Finally, with 0.1, clang produces

.LCPI0_0:
        .quad   0x3fb999999999999a              # double 0.10000000000000001
...

        .cfi_def_cfa_register %rbp
        cvtss2sd        %xmm0, %xmm0
        mulsd   .LCPI0_0(%rip), %xmm0
        cvtsd2ss        %xmm0, %xmm0
        popq    %rbp
        .cfi_def_cfa %rsp, 8
        retq

Notice, the conversions in this case.  If numerical accuracy
is unimportant but speed is, then using 0.1f seems to be the
best choice.  If numerical accuracy is important, one probably
use double from the get-go.

If you are going to work with floating point arithmetic,
then you should read Goldberg's paper.  In fact, you
will likely want to read it multiple times.

-- 
steve
Technical Details
Message-ID: <11bthbd$2mfvi$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-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> <11brkco$21oj5$1@dont-email.me> <11bs4tj$26daj$1@dont-email.me> <11bs613$26knh$1@dont-email.me> <11bt7ff$2i0ag$1@dont-email.me> <11bte4h$2lmho$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: "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu>
Newsgroups: comp.lang.c
Subject: Re: .5 against 0.5f
Date: Wed, 28 Oct 2026 19:10:37 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 116
Message-ID: <11bthbd$2mfvi$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> <11brkco$21oj5$1@dont-email.me>
	<11bs4tj$26daj$1@dont-email.me> <11bs613$26knh$1@dont-email.me>
	<11bt7ff$2i0ag$1@dont-email.me> <11bte4h$2lmho$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 28 Oct 2026 19:10:38 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2834418"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19fEypG6/axWb8m0wLtSRUi"; posting-host="1bf873808c53778ca982afc43c23d859"
User-Agent: Pan/0.164 (Kupiansk)
Cancel-Lock: sha1:ACMIhV4kH9DB33Wubb+hwMILfPQ= sha256:rs0qH6MJsekGpjE+FmwpNhKb7xpcjezN7B6XOF8lbfs=
	sha1:t+kABdUBxUtb1TEXJ4KT4/FdaiY= sha256:P05Kcnyy/5tu8BoYcP23I4xPTY7fyivt+BzexCxJelU=