🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1103

Re: .5 against 0.5f

#1103
From: David Brown
Date: Thu, 29 Oct 2026 08:30
95 lines
5456 bytes
On 29/10/2026 00:08, Janis Papanagnou wrote:
> On 2026-10-28 21:27, Keith Thompson wrote:
>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> [...]

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

Do you mean that you think it would have been better if the rules of C 
were different, so that the context always determined how "0.1" was to 
be interpreted?  That's not an unreasonable feature for a programming 
language - AFAIUI, that is how numeric literals work in Ada.  But it is 
not how things work in C, where the value and type of an expression is 
determined entirely by the expression itself, independently of the context.

I hate to try to speculate as to whether the C philosophy or the Ada 
philosophy here is more common - but I don't know of any languages other 
than Ada that make the type of literals dependent on their use or 
context.  There are of course many languages that might follow the Ada 
path, unbeknownst to me.  (You can have fun making class types in C++, 
and no doubt other languages, that emulate this behaviour, but it is not 
part of the language.)

So when you write "float x = 0.1;", the 0.1 is a value of type double. 
It is converted to float by the assignment operator.

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

"double d1 = 0.1;" and "double d2 = 0.1f;" do not put the same value in 
"d1" and "d2".  Often the exact value does not matter much, and usually 
when it /does/ matter, the double-precision version is what you want. 
But sometimes people are interested in highly reproducible results from 
their floating point - and that means being precise about the types you 
want.

I agree that is would be very unusual to want to initialise a 
double-precision variable with a single-precision floating constant.  I 
can't think of a circumstance where I would have reason to do so 
directly.  I /can/ think of indirect situations:

	#define scale_factor 1.234f
	...
	#if USE_HIGH_PRECISION
		typedef double calculation_t;
	#else
		typedef float calculation_t;
	#endif
	...
	calculation_t sf = scale_factor;

It is more likely that I'd make "scale_factor" a const of type 
"calculation_t", initialised from "1.234".  But perhaps that macro 
constant came from third-party code.


For more general use of single-precision floating constants, the 
use-cases are clearer.

First, there is the obvious one that regardless of how the floating 
constant is used, it is clear to the reader that 0.1f is single 
precision.  In contexts where speed matters (such as for limited 
microcontrollers, or where you hope to use SIMD acceleration) you do not 
want to use double precision accidentally - being explicit is better.

Secondly, narrowing conversions are not allowed in constexpr 
initialisations - you cannot write "constexpr float f = 0.1;".  (In C++, 
this also applies to braced initialisation - and is a good reason to use 
such initialisation style.)  Static analysis tools may also complain 
about narrowing initialisations.

And of course we have already covered their use in expressions.

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

Sure.  I haven't written "double d = 0.1f;" in my code either.
Technical Details
Message-ID: <11bv06a$34ll3$2@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> <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!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: .5 against 0.5f
Date: Thu, 29 Oct 2026 09:30:02 +0100
Organization: A noiseless patient Spider
Lines: 95
Message-ID: <11bv06a$34ll3$2@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> <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; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 29 Oct 2026 08:30:03 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3298979"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+j6EEdooOStmIjRUdDPFQtaRWESl98iUQ="; posting-host="ee186c62f9168531a82b8aa89ffdfee0"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:p1UmpQ3RBJv629XEbj0T7X4Ni68= sha256:GGWqRQg3BwjQ0WSTrMbYFzBxmHV1UlANg1t8k7vV8bM=
	sha1:YeBQYEUaXrko3p/9OfE2u8KfYR0= sha256:791OQy3r0wihoFV9oFbsR77UrLYgje4eIC8R1byXijI=
In-Reply-To: <11btv9c$139ta$12@dont-email.me>
Content-Language: en-GB, nb-NO