🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #991

Re: .5 against 0.5f

#991
From: Keith Thompson
Date: Wed, 28 Oct 2026 23:56
116 lines
6664 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 2026-10-28 21:27, Keith Thompson wrote:
>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> [...]
>>> The thought of something "implicit" going on here is a thought that
>>> might stem from (knowing) the inherent mechanisms and quirks of the
>>> C-language not necessarily what one would logically expect.
>> My logical expectation is based on knowing C.
>
> And knowing also a lot of the internals, and C-standards documents,
> I'm sure. (My point was just that "normal" C-programmers might not
> have that background and would "logically" assume different. - But
> since you had clarified the word by using a more appropriate one in
> this post I'm fine with that now. Thanks.)
>
>>> My expectation (for sensibly defined programming languages) would be
>>> that literals for two different "real numbers" types are generic WRT
>>> those types; they should be usable [in "C"] without loss (or kludges)
>>> with 'float' and 'double'.
>> Sure, a compiler can generate code to store (float)0.1 in f without
>> doing a conversion, as long as the semantics are consistent. 
>
> Only that my view here is that you need a "conversion" in both cases,
> since you primarily have to convert from a string representation of
> the literal to the appropriate internal (binary) form. And given the
> _program contexts_ you can do _the one_ appropriate conversion for
> the types involved (i.e. Text->Float and Text->Double, respectively).

I don't think of the process that takes a source code construct
like `0.1` and yields a value of 0.1 at run time as a "conversion".
It's just the meaning of the token.  "Text" is not a C type.  (The
constant might happen to be held in a C string at some point, but
only if the compiler is written in C.)

> Note: my argumentation is based on questioning the existence of 'F'.
> So I'd appreciate to hear of any enlightening application cases, ones
> that would be impossible when 'F' in literals wouldn't be present.

By 'F', you mean the 'f' or 'F' suffix on a floating constant, right?
(Your use of upper case threw me off for a moment.  No biggie.)

> (And, ideally, practically relevant usages. - We know that "C" is as
> it is, and as it is there's some unavoidable consequences from that.)

Yes, we know that from "C", which is the topic of this newsgroup.
I'm not sure what distinction you're making.  You seem to be
implying that C imposes some set of obscure rules on top of what
all programmers know.  0.0 is something that everyone recognizes,
while 0.0f is an obscure C feature.

I don't think of it that way.  C code is governed by the rules of C.

>> But 0.1f and 0.1 are of different types.  If they happen to behave
>> the same way in some context, it's a consequence of the C language
>> rules.
>
> Yes, sure. Only that I think that context should suffice to interpret
> "0.1" appropriately!

In many cases, yes, the final result is likely to be the same.
Even so, using 0.1 rather than 0.1f can result in less efficient
generated code if the compiler isn't sufficiently clever, or perhaps
even if it is.

And if I'm trying to use type float and avoid type double (as, for
example, David Brown has good reasons to do), writing `0.1f` is a
great way to avoid having to think about how clever the compiler
has to be to avoid unnecessary conversions.

>> Note in particular that this:
>>      double d = 0.1f;
>> has different semantics than
>>      double d = 0.1;
>> if double has more precision than float.
>
> Sure. But isn't that difference basically just a home-brewed C-issue?

"Home-brewed"?  It's a rule imposed by the C language.

> I'd be curious why one would write the former in the first place.
> Will a programmer or program designer gain something by assigning a
> 'float' constant literal to a 'double' variable?

Probably not.  The point is that there are real semantic differences
between 0.1f and 0.1.

On the other hand, if you want to print a float value using printf, it
will be converted to double.

    printf("%.55f\n%.55f\n", 0.1f, 0.1);

    0.1000000014901161193847656250000000000000000000000000000
    0.1000000000000000055511151231257827021181583404541015625

> I've never needed such, erm.., "hacks" ever in any of my C-code or
> in other languages. So I'm honestly asking.

It was an example, not meant to be a demonstration of useful code.
I could have mentioned that `sizeof 0.1f` is likely to be different
from `sizeof 0.1`, but that's even more unrealistic.

I think there's a common attitude that double is the "default" type
for floating-point constants.  That's a valid way to look at it, but
I suggest an equally valid way is to say that *there is no default*.

If I write 0.1f, I'm explicitly using type float.
If I write 0.1,  I'm explicitly using type double.
If I write 0.1L, I'm explicitly using type long double.

In one of the three cases, the suffix just happens to be empty.

(And again, I'm glossing over FLT_EVAL_METHOD.)

-- 
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: <11bu23t$2rgo2$5@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> <11brir0$1uh9q$4@kst.eternal-september.org> <11bt4ua$139ta$8@dont-email.me> <11btlrl$2ojn1$1@kst.eternal-september.org> <11btv9c$139ta$12@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:56:45 -0700
Organization: None to speak of
Lines: 116
Message-ID: <11bu23t$2rgo2$5@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>
	<11brir0$1uh9q$4@kst.eternal-september.org>
	<11bt4ua$139ta$8@dont-email.me>
	<11btlrl$2ojn1$1@kst.eternal-september.org>
	<11btv9c$139ta$12@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Wed, 28 Oct 2026 23:56:46 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="2999042"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/1Uu+VfbuV2oBHsYZRN1Ef"; posting-host="ac4e6fa4e505b2147f0d092402a060c4"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:GkBmpO69cMudSMMIgJdgA4fXlpI= sha256:hyQtqtAAfLDgCte/480Gm8BFqnFSSz+6z7VPKCmPUYE=
	sha1:2P3fQYdosQNdfPTaBh6k1MjLdAM= sha256:zcHidZ4kpwG7bPcpYEe/P7lNDWvk8aUvr5YQXGUgP/I=