Thread View: comp.lang.c
158 messages
158 total messages
Page 2 of 4
Started by fir
Tue, 27 Oct 2026 10:53
Page 2 of 4 • 158 total messages
Re: .5 against 0.5f
Author: Steven G. Kargl
Date: Wed, 28 Oct 2026 16:27
Date: Wed, 28 Oct 2026 16:27
21 lines
1944 bytes
1944 bytes
On Wed, 28 Oct 2026 07:55:11 +0100, fir wrote:
>> but i not tested/talked yet
> ai says even fast math not enebles that but there is a flag
>
>
> -fsingle-precision-constant.
It is a poor decision to depend on any specific compiler's
options unless you intend to never use any other compiler.
With clang, I see
% cc -o z.s -S -O3 -fsingle-precision-constant j8.c
cc: warning: optimization flag '-fsingle-precision-constant' is not
supported [-Wignored-optimization-argument]
HTH
--
steve
Re: .5 against 0.5f
Author: David Brown
Date: Wed, 28 Oct 2026 16:44
Date: Wed, 28 Oct 2026 16:44
32 lines
2930 bytes
2930 bytes
On 28/10/2026 17:27, Steven G. Kargl wrote: > On Wed, 28 Oct 2026 07:55:11 +0100, fir wrote: > > >>> but i not tested/talked yet >> ai says even fast math not enebles that but there is a flag >> >> >> -fsingle-precision-constant. > > It is a poor decision to depend on any specific compiler's > options unless you intend to never use any other compiler. It is fine to do so in non-portable code. Most of my work is specifically targeting gcc, and if the code is better (clearer, safer to maintain, more efficient) as a result of compiler-specific flags or compiler-specific extensions, that's fine. Of course, if the same results can be achieved without non-portable flags or extensions, that is better. The key use of flags like this is for microcontrollers with hardware support for single-precision floating point, but not for double precision - which then needs to be handled by software routines. The use of "-fsingle-precision-constant" is primarily for handling third-party code or other code that you don't want to change, but where you want to use single precision as much as possible. For your own code on such devices, you would use "f" suffixes. The "-Wdouble-promotion" flag is also very useful here - it will let you know if you have accidentally promoted a float (such as by using a double-precision constant in an expression).
Re: .5 against 0.5f
Author: fir
Date: Wed, 28 Oct 2026 18:15
Date: Wed, 28 Oct 2026 18:15
24 lines
2360 bytes
2360 bytes
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
Re: .5 against 0.5f
Author: fir
Date: Wed, 28 Oct 2026 18:47
Date: Wed, 28 Oct 2026 18:47
41 lines
2943 bytes
2943 bytes
fir pisze:
> 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
and i would say it is good to be able to express what one wants
and write 25*a, 25.0d *a, 25.0f *a //if there would be version for double
but for me its really weird that compiler cant 25.*a resolve as floats
if optimisation is on and put those conversions
same as 25 imo shouldn be treated as int if in runtime faster it would
be if that would be short long or char
imo constant is constant so 25.1 imo even should not represent nor float
nor double but probably the 'ideal' value
Re: .5 against 0.5f
Author: Steven G. Kargl
Date: Wed, 28 Oct 2026 19:10
Date: Wed, 28 Oct 2026 19:10
116 lines
4856 bytes
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
Re: .5 against 0.5f
Author: Michael S
Date: Wed, 28 Oct 2026 20:25
Date: Wed, 28 Oct 2026 20:25
30 lines
2283 bytes
2283 bytes
On Wed, 28 Oct 2026 08:39:23 +0100 David Brown <david.brown@hesbynett.no> wrote: > On 28/10/2026 01:49, Lawrence D’Oliveiro wrote: > > On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote: > > > >> However, if you need double precision, you shouldn't be using the > >> "f" suffix. > > > > Is there any situation where you *should* use the “f” suffix? > > > > I am thinking: no. > > Try thinking harder. > > 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. > > I'd still likely prefer to write it as (float)0.1 instead of 0.1f. It somewhat depends on my mood in specific day, but likelihood is certainly not in favor of 0.1f.
Re: .5 against 0.5f
Author: Keith Thompson
Date: Wed, 28 Oct 2026 20:27
Date: Wed, 28 Oct 2026 20:27
52 lines
3364 bytes
3364 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 2026-10-28 02:23, Keith Thompson wrote:
>> Lawrence D’Oliveiro <ldo@nz.invalid> writes:
>>> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote:
>>>> However, if you need double precision, you shouldn't be using the
>>>> "f" suffix.
>>>
>>> Is there any situation where you *should* use the “f” suffix?
>>>
>>> I am thinking: no.
>> If you're working with values of type float, the "f" suffix is more
>> explicit.
>> Logically, something like
>> float f = 0.1;
>> implies a conversion from double to float, which could be
>> non-trivial.
>
> I think "logically" is a misleading word here.
Perhaps "in the abstract machine" would have been clearer than
"logically".
In the abstract machine, 0.1 is of type double. The initialization
causes that value to be converted from double to float.
> 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.
> 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. 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.
Note in particular that this:
double d = 0.1f;
has different semantics than
double d = 0.1;
if double has more precision than float.
[...]
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: .5 against 0.5f
Author: Keith Thompson
Date: Wed, 28 Oct 2026 20:31
Date: Wed, 28 Oct 2026 20:31
32 lines
2497 bytes
2497 bytes
"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;
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: .5 against 0.5f
Author: Keith Thompson
Date: Wed, 28 Oct 2026 21:26
Date: Wed, 28 Oct 2026 21:26
27 lines
2376 bytes
2376 bytes
David Brown <david.brown@hesbynett.no> writes:
> On 28/10/2026 01:49, Lawrence D’Oliveiro wrote:
>> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote:
>>> However, if you need double precision, you shouldn't be using the
>>> "f" suffix.
>> Is there any situation where you *should* use the “f” suffix?
>> I am thinking: no.
>
> Try thinking harder.
>
> 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;
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: .5 against 0.5f
Author: Chris M. Thomasson
Date: Wed, 28 Oct 2026 21:34
Date: Wed, 28 Oct 2026 21:34
25 lines
2375 bytes
2375 bytes
On 10/28/2026 12:39 AM, David Brown wrote: > On 28/10/2026 01:49, Lawrence D’Oliveiro wrote: >> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote: >> >>> However, if you need double precision, you shouldn't be using the >>> "f" suffix. >> >> Is there any situation where you *should* use the “f” suffix? >> >> I am thinking: no. > > Try thinking harder. > > 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. > > Same here. I am mostly creating graphics related things. double precision in a GPU might not be there. Now, if I am doing something that needs higher precision, tends to be with deep zooms or granularity in a vector field, then double is okay. But I need to beware that the shaders might not support double.
Re: .5 against 0.5f
Author: Chris M. Thomasson
Date: Wed, 28 Oct 2026 21:36
Date: Wed, 28 Oct 2026 21:36
12 lines
1819 bytes
1819 bytes
On 10/27/2026 5:49 PM, Lawrence D’Oliveiro wrote: > On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote: > >> However, if you need double precision, you shouldn't be using the >> "f" suffix. > > Is there any situation where you *should* use the “f” suffix? > > I am thinking: no. Well, say you want the result from a frag shader to be pixel perfect to a CPU version of it. Using double on the CPU can cause a headache.
Re: .5 against 0.5f
Author: Lawrence D’Oliveiro
Date: Wed, 28 Oct 2026 21:54
Date: Wed, 28 Oct 2026 21:54
7 lines
1594 bytes
1594 bytes
On Wed, 28 Oct 2026 02:19:24 -0400, steve g wrote: > Try lisp, you can use fractions, ratios, very large doubles. It is > somewhat slow with floating point usually. Ratios I like. 1/100 - > that is it. Python has all that. Plus overloads for actual infix operators.
Re: .5 against 0.5f
Author: Chris M. Thomasson
Date: Wed, 28 Oct 2026 21:58
Date: Wed, 28 Oct 2026 21:58
10 lines
1848 bytes
1848 bytes
On 10/28/2026 2:54 PM, Lawrence D’Oliveiro wrote: > On Wed, 28 Oct 2026 02:19:24 -0400, steve g wrote: > >> Try lisp, you can use fractions, ratios, very large doubles. It is >> somewhat slow with floating point usually. Ratios I like. 1/100 - >> that is it. > > Python has all that. Plus overloads for actual infix operators. Python is pretty nice when I need to use it. Python 2 or Python 3? ;^)
Re: .5 against 0.5f
Author: Michael S
Date: Wed, 28 Oct 2026 22:12
Date: Wed, 28 Oct 2026 22:12
33 lines
2621 bytes
2621 bytes
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: > > On 28/10/2026 01:49, Lawrence D’Oliveiro wrote: > >> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote: > >>> However, if you need double precision, you shouldn't be using the > >>> "f" suffix. > >> Is there any situation where you *should* use the “f” suffix? > >> I am thinking: no. > > > > Try thinking harder. > > > > 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.
Re: .5 against 0.5f
Author: bartc
Date: Wed, 28 Oct 2026 22:33
Date: Wed, 28 Oct 2026 22:33
63 lines
3374 bytes
3374 bytes
On 28/10/2026 22:12, Michael S wrote:
> 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:
>>> On 28/10/2026 01:49, Lawrence D’Oliveiro wrote:
>>>> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote:
>>>>> However, if you need double precision, you shouldn't be using the
>>>>> "f" suffix.
>>>> Is there any situation where you *should* use the “f” suffix?
>>>> I am thinking: no.
>>>
>>> Try thinking harder.
>>>
>>> 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.
>
>
Yeah, intermediate calculations using 64 bits can give a different and
more precise result even after it has been converted to 32 bits.
For example this:
#include <stdio.h>
int main(void) {
float a = 0.1f * 0.1f;
float b = 0.1 * 0.1;
printf("%20.20f\n", a);
printf("%20.20f\n", b);
printf("%20.20f\n", 1 - a * 100);
printf("%20.20f\n", 1 - b * 100);
}
Displays:
0.01000000070780515671
0.00999999977648258209
-0.00000011920928955078
0.00000000000000000000
on gcc 9.x on x64.
It will need the compiler to do the reduction, but DB is likely using a
cross-compiler so it is not being done within target device,
Re: .5 against 0.5f
Author: Steven G. Kargl
Date: Wed, 28 Oct 2026 22:44
Date: Wed, 28 Oct 2026 22:44
40 lines
2709 bytes
2709 bytes
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.
--
steve
Re: .5 against 0.5f
Author: Keith Thompson
Date: Wed, 28 Oct 2026 23:05
Date: Wed, 28 Oct 2026 23:05
56 lines
3728 bytes
3728 bytes
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.
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'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.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: .5 against 0.5f
Author: Janis Papanagnou
Date: Wed, 28 Oct 2026 23:08
Date: Wed, 28 Oct 2026 23:08
62 lines
4089 bytes
4089 bytes
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). 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. (And, ideally, practically relevant usages. - We know that "C" is as it is, and as it is there's some unavoidable consequences from that.) > 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! > > 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? I've never needed such, erm.., "hacks" ever in any of my C-code or in other languages. So I'm honestly asking. Janis
Re: .5 against 0.5f
Author: Janis Papanagnou
Date: Wed, 28 Oct 2026 23:27
Date: Wed, 28 Oct 2026 23:27
51 lines
3460 bytes
3460 bytes
On 2026-10-28 23:44, Steven G. Kargl wrote:
> On Wed, 28 Oct 2026 13:31:06 -0700, Keith Thompson wrote:
>> "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes:
>>> [...]
>>>
>>> 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 basically agree with Keith here.
It's not about "realizing" the _mathematical_ meaning of "25 * a",
or about having a clearer/terser (less "fluffy") expression.
I'm sure the compiler would be able to create the correct internal
representation based on the program context; in that respect the
"25" should suffice, and not even impose any additional conversion
operation. IMO. (There might be some hindrance based on C's design
that I'm not aware of, though.)
From a programmers' perspective it always appeared useful to see
the type of the involved components in expressions. That's way I
write in every language "25.0" if I want to express a Real value.
(In some language it can even be functionally important, or even
be mandated.)
Where I disagree is that the 'f' would be meaningful as "concept"
(even if in "C" there's two "sorts" of floats, and two literals
for the same abstract numeric domain of real numbers). (But see
my disclaimer above about possible home-brewed hindrances in "C".)
Janis
Re: .5 against 0.5f
Author: 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 */
Re: .5 against 0.5f
Author: Keith Thompson
Date: Wed, 28 Oct 2026 23:56
Date: Wed, 28 Oct 2026 23:56
116 lines
6664 bytes
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 */
Re: .5 against 0.5f
Author: Steven G. Kargl
Date: Thu, 29 Oct 2026 00:22
Date: Thu, 29 Oct 2026 00:22
22 lines
2026 bytes
2026 bytes
On Wed, 28 Oct 2026 16:39:00 -0700, Keith Thompson wrote:
>
> (Also, you misspelled "ROFL", and you don't need parentheses on the
> return statement, particularly if you dislike fluff.)
Yeah, I was laugh too hard while typing ;-)
Parentheses is a habit from BSD kernel source file style guide.
% man 9 style
...
Values in return statements should be enclosed in parentheses.
I also have debugging macros RETURNI() that require parens.
--
steve
Re: .5 against 0.5f
Author: Tim Rentsch
Date: Thu, 29 Oct 2026 00:59
Date: Thu, 29 Oct 2026 00:59
28 lines
2439 bytes
2439 bytes
"Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes: > On Wed, 28 Oct 2026 16:39:00 -0700, Keith Thompson wrote: > >> (Also, you misspelled "ROFL", and you don't need parentheses on the >> return statement, particularly if you dislike fluff.) > > Yeah, I was laugh too hard while typing ;-) > > Parentheses is a habit from BSD kernel source file style guide. Now is a good time to start unlearning that habit. > % man 9 style > ... > > Values in return statements should be enclosed in parentheses. > > > I also have debugging macros RETURNI() that require parens. That strengthens the motivation not to use parentheses with return statements, to emphasize that they are different from the macros. Otherwise someone might wonder if there had been a #define return(x) return ...something involving x...
Re: .5 against 0.5f
Author: Steven G. Kargl
Date: Thu, 29 Oct 2026 01:50
Date: Thu, 29 Oct 2026 01:50
42 lines
2971 bytes
2971 bytes
On Wed, 28 Oct 2026 17:59:19 -0700, Tim Rentsch wrote: > "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes: > >> On Wed, 28 Oct 2026 16:39:00 -0700, Keith Thompson wrote: >> >>> (Also, you misspelled "ROFL", and you don't need parentheses on the >>> return statement, particularly if you dislike fluff.) >> >> Yeah, I was laugh too hard while typing ;-) >> >> Parentheses is a habit from BSD kernel source file style guide. > > Now is a good time to start unlearning that habit. Why? What harm do the parentheses cause? Unlearning is not likely to happen. I've been a long-time contributor to FreeBSD, and have no plans to stop. Feel free to check the git history on FreeBSD libm sources. My finger prints are all over the code. So, yeah, I know a enough about floating point arithmetic and C standard requirements to be dangerous. >> % man 9 style >> ... >> >> Values in return statements should be enclosed in parentheses. >> >> >> I also have debugging macros RETURNI() that require parens. > > That strengthens the motivation not to use parentheses with > return statements, to emphasize that they are different from > the macros. Do you ignore the style requirements of all the projects that you may have worked on, and simply use whatever your personal preferences are? -- steve
Re: .5 against 0.5f
Author: David Brown
Date: Thu, 29 Oct 2026 08:30
Date: Thu, 29 Oct 2026 08:30
95 lines
5456 bytes
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.
Re: .5 against 0.5f
Author: David Brown
Date: Thu, 29 Oct 2026 09:45
Date: Thu, 29 Oct 2026 09:45
79 lines
5074 bytes
5074 bytes
On 29/10/2026 02:50, Steven G. Kargl wrote: > On Wed, 28 Oct 2026 17:59:19 -0700, Tim Rentsch wrote: > >> "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes: >> >>> On Wed, 28 Oct 2026 16:39:00 -0700, Keith Thompson wrote: >>> >>>> (Also, you misspelled "ROFL", and you don't need parentheses on the >>>> return statement, particularly if you dislike fluff.) >>> >>> Yeah, I was laugh too hard while typing ;-) >>> >>> Parentheses is a habit from BSD kernel source file style guide. >> >> Now is a good time to start unlearning that habit. > > Why? What harm do the parentheses cause? I can't see that parentheses here cause any actual harm - but they are certainly unnecessary fluff, and do no good. Every extra level of parentheses adds to the cognitive effort required to understand and interpret an expression. It takes longer to read, and has a higher chance of misinterpretation. If the parenthesis are necessary - "(a + b) * c" - then this is clearly a cost worth paying. Sometimes parentheses are not necessary, but /reduce/ the cognitive effort, such as "(a > 2) && (a < 5)". Again, the parentheses are then a good thing (albeit subjectively). But in "return (x);", they add nothing but cognitive effort. The C programmer's brain has to consider and reject the possibility that this is a function call or a macro invocation. The effort is small, but nonetheless wasted. Let me put it another way. What benefit do you think there is from these parentheses? Why does the FreeBSD style guide recommend them? I can see benefits in consistency of style once that usage has been established, but why have them in the first place? > > Unlearning is not likely to happen. I've been a long-time > contributor to FreeBSD, and have no plans to stop. Feel > free to check the git history on FreeBSD libm sources. > My finger prints are all over the code. So, yeah, I know > a enough about floating point arithmetic and C standard > requirements to be dangerous. > >>> % man 9 style >>> ... >>> >>> Values in return statements should be enclosed in parentheses. >>> >>> >>> I also have debugging macros RETURNI() that require parens. >> >> That strengthens the motivation not to use parentheses with >> return statements, to emphasize that they are different from >> the macros. > > Do you ignore the style requirements of all the projects > that you may have worked on, and simply use whatever > your personal preferences are? > When working on code with particular style requirements, it makes sense to follow those style requirements - even if you disagree with the requirements. When working on other code, you are then freed from following rules that are unnecessary or counter-productive. If I were contributing code to FreeBSD and it /requires/ parentheses in return statements for the code to be accepted, I'd put parentheses in return statements. If it merely /recommends/ them, I would not do so - but would change my mind if other FreeBSD contributors made a fuss about it. In no other code would I use them, and in code reviews I would ask for them to be removed. But I would not bother about them in throw-away Usenet code, except if the post was complaining about other unnecessary "fluff" :-)
Re: .5 against 0.5f
Author: David Brown
Date: Thu, 29 Oct 2026 09:48
Date: Thu, 29 Oct 2026 09:48
31 lines
2548 bytes
2548 bytes
On 28/10/2026 21:25, Michael S wrote: > On Wed, 28 Oct 2026 08:39:23 +0100 > David Brown <david.brown@hesbynett.no> wrote: > >> On 28/10/2026 01:49, Lawrence D’Oliveiro wrote: >>> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote: >>> >>>> However, if you need double precision, you shouldn't be using the >>>> "f" suffix. >>> >>> Is there any situation where you *should* use the “f” suffix? >>> >>> I am thinking: no. >> >> Try thinking harder. >> >> 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. >> >> > > I'd still likely prefer to write it as (float)0.1 instead of 0.1f. > It somewhat depends on my mood in specific day, but likelihood is > certainly not in favor of 0.1f. > That seems a bit excessive to me, but okay. The principle is the same, making it clear to the reader and the compiler that no double-precision arithmetic is involved.
Re: .5 against 0.5f
Author: David Brown
Date: Thu, 29 Oct 2026 09:59
Date: Thu, 29 Oct 2026 09:59
40 lines
3065 bytes
3065 bytes
On 28/10/2026 23:12, Michael S wrote: > 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: >>> On 28/10/2026 01:49, Lawrence D’Oliveiro wrote: >>>> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote: >>>>> However, if you need double precision, you shouldn't be using the >>>>> "f" suffix. >>>> Is there any situation where you *should* use the “f” suffix? >>>> I am thinking: no. >>> >>> Try thinking harder. >>> >>> 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. > Yes, that is the most relevant usage. But I would not write "float f = 0.1;" either. "float f = 0.1f;" is more explicit, and it would not risk accidentally triggering any static analysis (or human analysis) that checks for unintended use of doubles. (gcc's "-Wdouble-promotion" does not trigger on "float f = 0.1;", but other tools might.)
Re: .5 against 0.5f
Author: David Brown
Date: Thu, 29 Oct 2026 10:01
Date: Thu, 29 Oct 2026 10:01
72 lines
3953 bytes
3953 bytes
On 28/10/2026 23:33, bartc wrote:
> On 28/10/2026 22:12, Michael S wrote:
>> 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:
>>>> On 28/10/2026 01:49, Lawrence D’Oliveiro wrote:
>>>>> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote:
>>>>>> However, if you need double precision, you shouldn't be using the
>>>>>> "f" suffix.
>>>>> Is there any situation where you *should* use the “f” suffix?
>>>>> I am thinking: no.
>>>>
>>>> Try thinking harder.
>>>>
>>>> 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.
>>
>>
>
> Yeah, intermediate calculations using 64 bits can give a different and
> more precise result even after it has been converted to 32 bits.
>
> For example this:
>
> #include <stdio.h>
>
> Â int main(void) {
> Â Â Â float a = 0.1f * 0.1f;
> Â Â Â float b = 0.1 * 0.1;
>
> Â Â Â printf("%20.20f\n", a);
> Â Â Â printf("%20.20f\n", b);
> Â Â Â printf("%20.20f\n", 1 - a * 100);
> Â Â Â printf("%20.20f\n", 1 - b * 100);
> Â }
>
>
> Displays:
>
> Â 0.01000000070780515671
> Â 0.00999999977648258209
> Â -0.00000011920928955078
> Â 0.00000000000000000000
>
> on gcc 9.x on x64.
>
> It will need the compiler to do the reduction, but DB is likely using a
> cross-compiler so it is not being done within target device,
Generally, yes. (Though for this code, the compiler would pre-calculate
the results at compile time.) My calculations need to be as precise as
required for their use - but anything beyond that is just a waste. And
if the microcontroller has hardware single-precision floating point but
no double-precision, that wasted runtime is massive.
Re: .5 against 0.5f
Author: makendo
Date: Thu, 29 Oct 2026 10:04
Date: Thu, 29 Oct 2026 10:04
16 lines
1899 bytes
1899 bytes
> I ordered a couple of blu-rays from amazon last week, and the > confirmation email had the following for the total price: > > Total > 75.71000000000001 USD > > Appears they may be using FP instead of integer fixed point arithmetic > for pricing. And this is why you should use the lowest value denominator when storing monetary values -- store USD values in cents, CNY values in fen, EUR values in cents, etc... I envy in particular the Japanese on this -- they don't have monetary units smaller than 1 yen, and as such misuse of floats in monetary calculations are far less likely in Japan.
Re: .5 against 0.5f
Author: David Brown
Date: Thu, 29 Oct 2026 10:06
Date: Thu, 29 Oct 2026 10:06
33 lines
2783 bytes
2783 bytes
On 28/10/2026 22:34, Chris M. Thomasson wrote: > On 10/28/2026 12:39 AM, David Brown wrote: >> On 28/10/2026 01:49, Lawrence D’Oliveiro wrote: >>> On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote: >>> >>>> However, if you need double precision, you shouldn't be using the >>>> "f" suffix. >>> >>> Is there any situation where you *should* use the “f” suffix? >>> >>> I am thinking: no. >> >> Try thinking harder. >> >> 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. >> >> > > Same here. I am mostly creating graphics related things. double > precision in a GPU might not be there. Now, if I am doing something that > needs higher precision, tends to be with deep zooms or granularity in a > vector field, then double is okay. But I need to beware that the shaders > might not support double. You can also see it in SIMD. A 128-bit SIMD register might be useable for 4 single-precision values and parallel operations, while only 2 can be handled for double-precision. Obviously details will vary by SIMD implementation, but singles are going to be at least twice as fast in general.
Re: .5 against 0.5f
Author: Keith Thompson
Date: Thu, 29 Oct 2026 11:54
Date: Thu, 29 Oct 2026 11:54
35 lines
2998 bytes
2998 bytes
David Brown <david.brown@hesbynett.no> writes:
> 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.
Strictly speaking, the type of a numeric literal in Ada is
<universal_integer> or <universal_real>. Both types are anonymous,
have effectively infinite precision, and exist only at compile time.
There's an implicit conversion to the type determined by the context
(one of the few implicit conversions in Ada).
The effect is similar to what you'd get if the type of a literal
depending on its context.
[...]
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: .5 against 0.5f
Author: Keith Thompson
Date: Thu, 29 Oct 2026 11:59
Date: Thu, 29 Oct 2026 11:59
19 lines
2235 bytes
2235 bytes
David Brown <david.brown@hesbynett.no> writes:
> On 28/10/2026 21:25, Michael S wrote:
[...]
>> I'd still likely prefer to write it as (float)0.1 instead of 0.1f.
>> It somewhat depends on my mood in specific day, but likelihood is
>> certainly not in favor of 0.1f.
>>
> That seems a bit excessive to me, but okay. The principle is the
> same, making it clear to the reader and the compiler that no
> double-precision arithmetic is involved.
Except that, at least in the abstract machine, `(float)0.1` produces
a value of type double and converts it to float. It's likely that
a compiler would generate the same code as for `0.1f`, but I'd want
to confirm that if it mattered. (Actually, I'd just write `0.1f`.)
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: .5 against 0.5f
Author: Keith Thompson
Date: Thu, 29 Oct 2026 12:01
Date: Thu, 29 Oct 2026 12:01
22 lines
2114 bytes
2114 bytes
makendo <makendo@makendo.invalid> writes:
>> I ordered a couple of blu-rays from amazon last week, and the
>> confirmation email had the following for the total price:
>> Total
>> 75.71000000000001 USD
>> Appears they may be using FP instead of integer fixed point
>> arithmetic
>> for pricing.
>
> And this is why you should use the lowest value denominator when
> storing monetary values -- store USD values in cents, CNY values
> in fen, EUR values in cents, etc...
[...]
That's not good enough. Discounts, sales taxes, and compound
interest can easily result in amounts that are not multiples of
one cent, which must then be rounded or truncated to the nearest
cent following whatever financial rules are in effect.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: .5 against 0.5f
Author: David Brown
Date: Thu, 29 Oct 2026 12:55
Date: Thu, 29 Oct 2026 12:55
33 lines
2844 bytes
2844 bytes
On 29/10/2026 13:01, Keith Thompson wrote: > makendo <makendo@makendo.invalid> writes: >>> I ordered a couple of blu-rays from amazon last week, and the >>> confirmation email had the following for the total price: >>> Total >>> 75.71000000000001 USD >>> Appears they may be using FP instead of integer fixed point >>> arithmetic >>> for pricing. >> >> And this is why you should use the lowest value denominator when >> storing monetary values -- store USD values in cents, CNY values >> in fen, EUR values in cents, etc... > [...] > > That's not good enough. Discounts, sales taxes, and compound > interest can easily result in amounts that are not multiples of > one cent, which must then be rounded or truncated to the nearest > cent following whatever financial rules are in effect. > What is "good enough" depends on the context. For a lot of situations, floating point, or rounding to the nearest dollar (or thousand dollars, or pounds, or whatever) can be fine. You can happily use values in cents for your shop prices. But for accurate financial records used in accounting, tax records, banking, etc., you are correct that it is not that simple. Countries and groups of countries all have their standards that you need to conform to. Typically the way to get correct values is to keep everything as large integer types of scaled currency denominations - like a thousandth of a cent.
Re: .5 against 0.5f
Author: bartc
Date: Thu, 29 Oct 2026 14:32
Date: Thu, 29 Oct 2026 14:32
51 lines
3495 bytes
3495 bytes
On 29/10/2026 12:55, David Brown wrote: > On 29/10/2026 13:01, Keith Thompson wrote: >> makendo <makendo@makendo.invalid> writes: >>>> I ordered a couple of blu-rays from amazon last week, and the >>>> confirmation email had the following for the total price: >>>>     Total >>>>     75.71000000000001 USD >>>> Appears they may be using FP instead of integer fixed point >>>> arithmetic >>>> for pricing. >>> >>> And this is why you should use the lowest value denominator when >>> storing monetary values -- store USD values in cents, CNY values >>> in fen, EUR values in cents, etc... >> [...] >> >> That's not good enough. Discounts, sales taxes, and compound >> interest can easily result in amounts that are not multiples of >> one cent, which must then be rounded or truncated to the nearest >> cent following whatever financial rules are in effect. >> > > What is "good enough" depends on the context. For a lot of situations, > floating point, or rounding to the nearest dollar (or thousand dollars, > or pounds, or whatever) can be fine. You can happily use values in > cents for your shop prices. For /storing/ values in some database, then float binary will be fine, evening using dollars and decimals cents such as $1.99. But there are a couple of provisos: * There is limited precision of some 22 or 23 bits, which caps the magnitude that can be represented * It must be stipulated that the fractional part always represents between 0 and 99 hundredths of a dollar, even if the actual value cannot be exact (so 0.75 can be stored exactly, but not 0.74). There will never be a value halfway between two consecutive cents. With those in place, then any stored value can be converted to an exact number of cents (for example, (int)(x*100+0.5) for positive x), or to some other exact representation. Working directly with such float values is tricky. Oh, there is one more proviso: if printing such a value, you must only show 2 decimal places, not 14! So you don't get stupid output like 0.74000000953674316 USD.
Re: .5 against 0.5f
Author: Dan Cross
Date: Thu, 29 Oct 2026 14:56
Date: Thu, 29 Oct 2026 14:56
108 lines
5923 bytes
5923 bytes
In article <11bv4jd$37ch9$1@dont-email.me>, David Brown <david.brown@hesbynett.no> wrote: >> [snip] >> Why? What harm do the parentheses cause? > >I can't see that parentheses here cause any actual harm - but they are >certainly unnecessary fluff, and do no good. I agree; in my own code, I do not generally use parentheses around the expression I give in a `return` statement. But often our code is not our own, in the sense that we're constrained by working on a much larger project and there are externally imposed constraints that we must honor. I don't have to like them but, but I do have to abide by them. >Every extra level of parentheses adds to the cognitive effort required >to understand and interpret an expression. It takes longer to read, and >has a higher chance of misinterpretation. If the parenthesis are >necessary - "(a + b) * c" - then this is clearly a cost worth paying. >Sometimes parentheses are not necessary, but /reduce/ the cognitive >effort, such as "(a > 2) && (a < 5)". Again, the parentheses are then a >good thing (albeit subjectively). > >But in "return (x);", they add nothing but cognitive effort. The C >programmer's brain has to consider and reject the possibility that this >is a function call or a macro invocation. The effort is small, but >nonetheless wasted. > >Let me put it another way. What benefit do you think there is from >these parentheses? Why does the FreeBSD style guide recommend them? I >can see benefits in consistency of style once that usage has been >established, but why have them in the first place? I mostly agree with you. But when working on a large project, that spans many decades, with many different contributers, having uniformity is good. You mention cogitive load, but I submit that it is strictly worse in that regard to have to deal with multiple stylistic differences based on individual preference. My example is the Google monorepo. When I left Google, it contained about 2BLOC of code. All of which more or less conformed to Google's mandatory style "guides" (which were only "guides" insofar as allowing people to get explicit exemptions for very specific reasons from a small group of extremely senior engineers: like, Sanjay Ghemawat and Jeff Dean type of senior). Personally, I despised the style rules for almost all languages Google used; I thought the code was ugly. *But*, after a few weeks working in the codebase, they became all but invisible; I had to consciously think about them to remember my aesthetic objections. Furthermore, I claim that rigid adherence to _a_ set of rules, no matter how ugly I thought they were, was a big factor in allowing Google to scale to tends of thousands of engineers working concurrently in a multi-billion line codebase. Put another way, it is more important to have _a_ style that is used uniformly, while it is relatively less important what rules that style imposes. >>>> [snip] >>>> % man 9 style >>>> ... >>>> >>>> Values in return statements should be enclosed in parentheses. >>>> >>>> >>>> I also have debugging macros RETURNI() that require parens. >>> >>> That strengthens the motivation not to use parentheses with >>> return statements, to emphasize that they are different from >>> the macros. A metanote, not to David, but of course, reference to the `style(9)` man page _strengthens_ the argument for parenthesizing return'ed expressions when working with FreeBSD code. In fairness, it wasn't clear that that was the original context. >> Do you ignore the style requirements of all the projects >> that you may have worked on, and simply use whatever >> your personal preferences are? > >When working on code with particular style requirements, it makes sense >to follow those style requirements - even if you disagree with the >requirements. When working on other code, you are then freed from >following rules that are unnecessary or counter-productive. > >If I were contributing code to FreeBSD and it /requires/ parentheses in >return statements for the code to be accepted, I'd put parentheses in >return statements. If it merely /recommends/ them, I would not do so - >but would change my mind if other FreeBSD contributors made a fuss about >it. In no other code would I use them, and in code reviews I would ask >for them to be removed. > >But I would not bother about them in throw-away Usenet code, except if >the post was complaining about other unnecessary "fluff" :-) I do not find the amount of heat this has generated particularly useful. The parentheses may be superfluous, but they're not wrong, and if OP prefers to include them, it does no harm; in this case, there's a very good reason: OP works with FreeBSD code often, which mandates them; maintaining two separate styles to appease the USENET crowd is probably more cognitive effort than mentally blocking them out for a `return`. - Dan C.
Re: .5 against 0.5f
Author: James Kuyper
Date: Thu, 29 Oct 2026 15:56
Date: Thu, 29 Oct 2026 15:56
25 lines
2592 bytes
2592 bytes
On 2026-10-28 21:50, Steven G. Kargl wrote: > On Wed, 28 Oct 2026 17:59:19 -0700, Tim Rentsch wrote: > >> "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes: >> >>> On Wed, 28 Oct 2026 16:39:00 -0700, Keith Thompson wrote: >>> >>>> (Also, you misspelled "ROFL", and you don't need parentheses on the >>>> return statement, particularly if you dislike fluff.) >>> >>> Yeah, I was laugh too hard while typing ;-) >>> >>> Parentheses is a habit from BSD kernel source file style guide. >> >> Now is a good time to start unlearning that habit. > > Why? What harm do the parentheses cause? They can lead to people thinking that return is a function, even if they nominally know better. What would you think of return (a=b).retval; You might never write such code, and might never read it either, but you should have no trouble parsing it. Do you?
Re: .5 against 0.5f
Author: Steven G. Kargl
Date: Thu, 29 Oct 2026 16:34
Date: Thu, 29 Oct 2026 16:34
34 lines
3018 bytes
3018 bytes
On Thu, 29 Oct 2026 10:45:16 +0100, David Brown wrote:
>
> Let me put it another way. What benefit do you think there is from
> these parentheses? Why does the FreeBSD style guide recommend them? I
> can see benefits in consistency of style once that usage has been
> established, but why have them in the first place?
>
As we all know, FreeBSD is derived from software developed by
the CSRG at UC Berkeley during the 70's. FreeBSD adopted the
Kernel Normal Format (KNF) from CSRG. You will need to contact
a former member of CSRG such as Bostik, McKusick, Joy, Leffler,
etc. and them why KNF requires ().
Perhaps, the C compiler from 40 or so years ago required
parentheses in a return statement. Or, just maybe, members of
CSRG read K&R and noticed the authors of the book consistently
use paretheses in their examples. Ah yes, let's look at
the documentation:
% man 9 style
HISTORY
This manual page was originally based on the src/admin/style/style file
from the 4.4BSD-Lite2 release, with frequent updates to reflect the
current practice and desire of the FreeBSD project.
src/admin/style/style is a codification by the CSRG of the programming
style of Ken Thompson and Dennis Ritchie in Version 6 AT&T UNIX.
So, it seems to be a preference of Ritchie.
--
steve
Re: .5 against 0.5f
Author: Janis Papanagnou
Date: Thu, 29 Oct 2026 16:58
Date: Thu, 29 Oct 2026 16:58
49 lines
3401 bytes
3401 bytes
On 2026-10-29 02:50, Steven G. Kargl wrote: > On Wed, 28 Oct 2026 17:59:19 -0700, Tim Rentsch wrote: >> "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes: >>> On Wed, 28 Oct 2026 16:39:00 -0700, Keith Thompson wrote: >>> >>>> [...], and you don't need parentheses on the >>>> return statement, particularly if you dislike fluff.) >>> >>> [...] >>> >>> Parentheses is a habit from BSD kernel source file style guide. >> >> Now is a good time to start unlearning that habit. > > Why? What harm do the parentheses cause? > > Unlearning is not likely to happen. [...] I understand that long fostered habits are hard to change, so I certainly wouldn't criticize that; and especially since they do no harm as you say. (They're only "unnecessary fluff".) >>> [BSD style] >>> % man 9 style >>> ... >>> Values in return statements should be enclosed in parentheses. >>> [...] >> [...] > > Do you ignore the style requirements of all the projects > that you may have worked on, and simply use whatever > your personal preferences are? Following some projects' style requirements is certainly the most important argument here! Myself I started with "C" using parenthesis - and I wondered why! The reason (in my case) was obviously that this had been the style that was regularly used in the _K&R book_ (where I learned the "C" language from). But at some point (meanwhile decades ago) when I got aware that the parenthesis are unnecessary I immediately switched to the (IMO) much cleaner form without the spurious parenthesis. (There's already far too many [mandatory] parenthesis in the language, to my taste.) Janis
Re: .5 against 0.5f
Author: Janis Papanagnou
Date: Thu, 29 Oct 2026 17:55
Date: Thu, 29 Oct 2026 17:55
149 lines
8526 bytes
8526 bytes
On 2026-10-29 00:56, Keith Thompson wrote:
> 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. [...]
Sure. I was speaking about source code text, that gets transformed
to some binary representation by the compiler. I took the view from
a programmer's perspective of a language; to be clear, not mandating
(as some here do) that a programmer should read the C-standard to
learn and understand the language, but as more typical, a textbook.
>
>> 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.)
Yes. (Didn't intend to confuse people.) I'm regularly using (in "C")
capital letters for such qualifiers, so that they better stand out.
The 'L' for example (that I use more often; 'F' I've never used since
I also rarely do 'float' operations) is often (depending on the font)
harder to distinguish (from '1') if written in lower-case ('l').
>
>> (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.
I wanted to avoid reflexes like "accept how it is or leave" comments.
(I obviously failed with making that intent clear.)
As I've explained above, I was looking at it from a programmer's
perspective, not from someone who has the C-standard as bible on his
nightstand. With experiences from many other languages the "F-topic",
its necessity, is not that clear if we look at "C" conceptionally.
> 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 think 0 is a bad example, for some reasons in "C" and general.)
That "0.1" is (or may be, depending on the actual 'float' and 'double'
characteristics) effectively something different than "0.1F" is the
trivial part, unnecessary to explain. I had expanded below what was
bothering me.
>
> 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.
Actually I had been thinking about applications that David might
have - I recall he once said about him using 'float' to match some
controller hardware restrictions and stay efficient (or some such).
And while the initialization sample (below) did not make any sense
to me I was then thinking about expressions with mixed types where
it could make sense. But the consequences did also not make "much"
sense to me when thinking about it. Two basic cases appeared to me.
One was having expressions with mixed types (float and double);
in the micro-controller-case that would anyway have to be handled
in a sub-optimal way (performance-wise) when 'double's appear. And
in case of using just 'float' variables with non-F-tagged constants
my expectation would be that the compiler would/should take care of
the appropriate, sensible, efficient use of the constant. (Operating
in 'float' precision but interpreting "Real" literals as 'double'?!)
You say, "avoid having to think about how clever the compiler" is;
I understand that. But wouldn't it be more sensible to compose your
"Real" expressions in appropriate parts that reflect and guarantee
the precision explicitly? (As "C" is as it is it might be appealing
to use the 'F' instead, I admit. As said I was principally question
the necessity of 'F' in "C" from a design perspective.)
>
>>> 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 meant; is it a "home-brewed" consequence of the C-language design.)
>
>> 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.
Sure.
Janis
> [...]
Re: .5 against 0.5f
Author: Steven G. Kargl
Date: Thu, 29 Oct 2026 17:57
Date: Thu, 29 Oct 2026 17:57
43 lines
3249 bytes
3249 bytes
On Thu, 29 Oct 2026 11:56:38 -0400, James Kuyper wrote: > On 2026-10-28 21:50, Steven G. Kargl wrote: >> On Wed, 28 Oct 2026 17:59:19 -0700, Tim Rentsch wrote: >> >>> "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes: >>> >>>> On Wed, 28 Oct 2026 16:39:00 -0700, Keith Thompson wrote: >>>> >>>>> (Also, you misspelled "ROFL", and you don't need parentheses on the >>>>> return statement, particularly if you dislike fluff.) >>>> >>>> Yeah, I was laugh too hard while typing ;-) >>>> >>>> Parentheses is a habit from BSD kernel source file style guide. >>> >>> Now is a good time to start unlearning that habit. >> >> Why? What harm do the parentheses cause? > > They can lead to people thinking that return is a function, even if they > nominally know better. What would you think of > > return (a=b).retval; > > You might never write such code, and might never read it either, but you > should have no trouble parsing it. Do you? Never written nor seen such a line of code. Of course, a random line of code taken out of context may have many interpretations or no interpretation at all. I likely know less than 25% of what C can do as I am primarily interested in only C23 7.12 and Annex F and G. I do find it interesting that in a newgroup purportedly concerned with discussions about the C programming language, I seem to have been the only person in this thread to show a complete self-contained piece of code so fir can see the consequences of not understanding floating point arithmetic. -- steve
Re: .5 against 0.5f
Author: Janis Papanagnou
Date: Thu, 29 Oct 2026 18:57
Date: Thu, 29 Oct 2026 18:57
48 lines
3405 bytes
3405 bytes
On 2026-10-29 09:30, David Brown wrote: > On 29/10/2026 00:08, Janis Papanagnou wrote: >> On 2026-10-28 21:27, Keith Thompson wrote: >> [...] >>> 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? (I think my opinion here is irrelevant since "C" is what it is.) But, yes, my question was whether it would haven been necessary (language design-wise!) to have those tags in the first place. > 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 - (I also have no interest in speculations.) > 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.) (Here I had started writing examples from other languages, but it got long and no end to see, and it's anyway most likely considered off-topic, so I decided to just stop here.) Thanks for your elaborations on the question. Janis > [...]
Re: .5 against 0.5f
Author: Dan Cross
Date: Thu, 29 Oct 2026 19:49
Date: Thu, 29 Oct 2026 19:49
63 lines
2632 bytes
2632 bytes
In article <11bvqbn$3g0tj$1@dont-email.me>,
James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
>On 2026-10-28 21:50, Steven G. Kargl wrote:
>> On Wed, 28 Oct 2026 17:59:19 -0700, Tim Rentsch wrote:
>>
>>> "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes:
>>>
>>>> On Wed, 28 Oct 2026 16:39:00 -0700, Keith Thompson wrote:
>>>>
>>>>> (Also, you misspelled "ROFL", and you don't need parentheses on the
>>>>> return statement, particularly if you dislike fluff.)
>>>>
>>>> Yeah, I was laugh too hard while typing ;-)
>>>>
>>>> Parentheses is a habit from BSD kernel source file style guide.
>>>
>>> Now is a good time to start unlearning that habit.
>>
>> Why? What harm do the parentheses cause?
>
>They can lead to people thinking that return is a function, even if they
>nominally know better.
What? This is an incredibly weak argument.
If someone thought that `return` was a function, then why
wouldn't they think that `if` or `while` were also functions?
Or `sizeof`, and so on?
>What would you think of
>
> return (a=b).retval;
>
>You might never write such code, and might never read it either, but you
>should have no trouble parsing it. Do you?
I would think that code is bad, and unnecessarily obscure, but
perhaps someone _would_ write this code.
struct v {
int retval;
};
struct v a;
int
something(void)
{
struct v b;
b.retval = 1;
return (a=b).retval;
}
Of course, it would make no difference if the `return` statement
were written as,
return ((a=b).retval);
But again, the whole premise (that a programmer familiar with C
would ever think that `return` is a function) is silly.
- Dan C.
Re: .5 against 0.5f
Author: David Brown
Date: Thu, 29 Oct 2026 19:56
Date: Thu, 29 Oct 2026 19:56
25 lines
2566 bytes
2566 bytes
On 29/10/2026 19:57, Janis Papanagnou wrote: > On 2026-10-29 09:30, David Brown wrote: >> On 29/10/2026 00:08, Janis Papanagnou wrote: >>> On 2026-10-28 21:27, Keith Thompson wrote: >>> [...] >>>> 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? > > (I think my opinion here is irrelevant since "C" is what it is.) There's nothing wrong with having an opinion on what might or might not have been better, or what you might like in other languages. The pointless discussions come from claiming C can be, or needs to be, "fixed". But it's fine to stop here if you like.
Re: .5 against 0.5f
Author: Tim Rentsch
Date: Thu, 29 Oct 2026 20:39
Date: Thu, 29 Oct 2026 20:39
7 lines
1774 bytes
1774 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes: > 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. In C the word is constants, not literals.
Re: .5 against 0.5f
Author: Janis Papanagnou
Date: Thu, 29 Oct 2026 21:55
Date: Thu, 29 Oct 2026 21:55
16 lines
1918 bytes
1918 bytes
On 2026-10-29 20:49, Dan Cross wrote: > [...] > > But again, the whole premise (that a programmer familiar with C > would ever think that `return` is a function) is silly. Yes, that's hard to believe. I've heard the argument somehow reversed, alike "You don't need the parenthesis since 'return' is a statement (and no function)." IOW, it was not about mistaking 'return' for a function, more to make its _character as statement_ clear (and by that, en passant, invalidate K&R's paragon which had been widely followed back then). Janis
Re: .5 against 0.5f
Author: Janis Papanagnou
Date: Thu, 29 Oct 2026 22:06
Date: Thu, 29 Oct 2026 22:06
17 lines
2246 bytes
2246 bytes
On 2026-10-29 20:56, David Brown wrote: > > There's nothing wrong with having an opinion on what might or might not > have been better, or what you might like in other languages. The > pointless discussions come from claiming C can be, or needs to be, "fixed". "Abandon all hope, ye who enter here." :-) > But it's fine to stop here if you like. Yes, I think it makes little sense. It's also hard to compare languages on a specific feature alone without taking its whole design into account. The 'F' tag is just a detail of the issue (or "issue", in quotes, if you prefer) with types in "C". (IMO) Janis
Re: .5 against 0.5f
Author: Janis Papanagnou
Date: Thu, 29 Oct 2026 22:19
Date: Thu, 29 Oct 2026 22:19
27 lines
2427 bytes
2427 bytes
On 2026-10-29 13:01, Keith Thompson wrote: > makendo <makendo@makendo.invalid> writes: >>> I ordered a couple of blu-rays from amazon last week, and the >>> confirmation email had the following for the total price: >>> Total >>> 75.71000000000001 USD >>> Appears they may be using FP instead of integer fixed point >>> arithmetic >>> for pricing. >> >> And this is why you should use the lowest value denominator when >> storing monetary values -- store USD values in cents, CNY values >> in fen, EUR values in cents, etc... > [...] > > That's not good enough. Discounts, sales taxes, and compound > interest can easily result in amounts that are not multiples of > one cent, which must then be rounded or truncated to the nearest > cent following whatever financial rules are in effect. We also have to consider conversions of currencies between states in international trade, or, at times, switch of a currency unit; e.g. 1 Euro = 1,95583 DEM (note the officially defined number of decimals). Janis
Re: .5 against 0.5f
Author: Tim Rentsch
Date: Thu, 29 Oct 2026 22:42
Date: Thu, 29 Oct 2026 22:42
57 lines
3964 bytes
3964 bytes
"Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes: > On Wed, 28 Oct 2026 17:59:19 -0700, Tim Rentsch wrote: > >> "Steven G. Kargl" <sgk@REMOVEtroutmask.apl.washington.edu> writes: >> >>> On Wed, 28 Oct 2026 16:39:00 -0700, Keith Thompson wrote: >>> >>>> (Also, you misspelled "ROFL", and you don't need parentheses on the >>>> return statement, particularly if you dislike fluff.) >>> >>> Yeah, I was laugh too hard while typing ;-) >>> >>> Parentheses is a habit from BSD kernel source file style guide. >> >> Now is a good time to start unlearning that habit. > > Why? What harm do the parentheses cause? Wrong question. The important question is what benefit do they give? Granted, there may be circumstances where there is some benefit. But what benefit justifies using parentheses in *all* circumstances? > Unlearning is not likely to happen. I've been a long-time > contributor to FreeBSD, and have no plans to stop. Feel > free to check the git history on FreeBSD libm sources. > My finger prints are all over the code. So, yeah, I know > a enough about floating point arithmetic and C standard > requirements to be dangerous. > >>> % man 9 style >>> ... >>> >>> Values in return statements should be enclosed in parentheses. >>> >>> >>> I also have debugging macros RETURNI() that require parens. >> >> That strengthens the motivation not to use parentheses with >> return statements, to emphasize that they are different from >> the macros. > > Do you ignore the style requirements of all the projects > that you may have worked on, and simply use whatever > your personal preferences are? Please note what I said. I did not say never to use parentheses. What I did say is that now is a good time to start unlearning the habit of (always) using parentheses. If you're writing code destined for FreeBSD, by all means put in the extra parentheses. On the other hand, if you're writing code to put in a posting for comp.lang.c, it's more common not to use extra parentheses. When in Rome, do as the Romans do. If you have a compelling reason to use such redundant parentheses here in the newsgroup, please tell us what it is. Otherwise, is it you who is ignoring the style norms of the group who will be reading your code?
Page 2 of 4 • 158 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads