Article View: comp.lang.c
Article #990Re: .5 against 0.5f
From: Keith Thompson
Date: Wed, 28 Oct 2026 23:39
Date: Wed, 28 Oct 2026 23:39
53 lines
3596 bytes
3596 bytes
"Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes:
> On Wed, 28 Oct 2026 13:31:06 -0700, Keith Thompson wrote:
>> "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes:
>>> 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.
>>
>> I disagree. Yes, the int constant 25 is implicitly converted to float
>> as part of the "usual arithmetic conversions". But I find
>> float x = 25.0 * a;
>> or even
>> float x = 25.0f * a;
>> clearer than
>> float x = 25 * a;
>
> ROTL. You find '25 * a' too difficult to realize
> that it is twenty-five times a. The 2 or 3 extra
> digits (i.e., '.', '0', and 'f') are nothing but
> fluff.
I didn't say that I find `25 * a` difficult to understand. I said
that I find `25.0 * a` clearer. Even if it's only very slightly
easier to understand, that's well worth the cost of typing two
extra characters.
In this particular case, the type "float" appears on the same line as
the constant. In other cases, you might have an isolated assignment
like `x = 25.0 * a;`. In such a case, I find the floating-point
constant helpful as a reminder that this is a floating-point
operation, not an integer operation.
(Also, you misspelled "ROFL", and you don't need parentheses on the
return statement, particularly if you dislike fluff.)
--
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:
<11bu12l$2rgo2$4@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-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> <11brkco$21oj5$1@dont-email.me> <11bs4tj$26daj$1@dont-email.me> <11bs613$26knh$1@dont-email.me> <11bt7ff$2i0ag$1@dont-email.me> <11btm2a$2ojn1$2@kst.eternal-september.org> <11bttrp$2qvg6$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!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:39:00 -0700 Organization: None to speak of Lines: 53 Message-ID: <11bu12l$2rgo2$4@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> <11brkco$21oj5$1@dont-email.me> <11bs4tj$26daj$1@dont-email.me> <11bs613$26knh$1@dont-email.me> <11bt7ff$2i0ag$1@dont-email.me> <11btm2a$2ojn1$2@kst.eternal-september.org> <11bttrp$2qvg6$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Wed, 28 Oct 2026 23:39:01 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="2999042"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/eRSXNN8irTiLTL4MM1hvU"; posting-host="ac4e6fa4e505b2147f0d092402a060c4" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:jjidGEXZAc5/Xo2pOQmswXQtPdM= sha256:EB4hPFAybawrtU/hkZqrhVO4nGJn8TL7uNPa1C+w4sE= sha1:eTETQYMIyGkPoooA0WV349VYQ7Q= sha256:vYQvrO3t7lxEA5ocdvNfjj3KHVwwAA0m3ay/Vc0P29M=