Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #70192

Re: printf format specifier changes

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: printf format specifier changes
Date 2015-09-10 15:17 -0700
Organization None to speak of
Message-ID <ln37ylor6c.fsf@kst-u.example.com> (permalink)
References (3 earlier) <msh2au$psh$1@dont-email.me> <7d3eddec-100b-4362-a987-c365c5a414cf@googlegroups.com> <1b85e519-b814-4570-b61b-705aca90a1b4@googlegroups.com> <lnegi6njjj.fsf@kst-u.example.com> <f3e9e095-260e-42e8-9fef-c9ea06e18e4f@googlegroups.com>

Show all headers | View raw


supercat@casperkitty.com writes:
> On Thursday, September 10, 2015 at 2:47:44 PM UTC-5, Keith Thompson wrote:
>> ANSI (and ISO) specified that float arguments are promoted to double,
>> and double and long double arguments are passed as they are.  The rules
>> are unambiguous.
>
> Unambiguous does not mean helpful, and the ambiguity is not so much in the
> prototype itself, but numeric promotions elsewhere.  On some platforms
> (*especially those without floating-point units*), the most efficient way
> to perform operations on floating-point values is to convert all values to
> an extended-precision type and perform computations with that type, and
> then coerce the value as needed when assigning it into a variable or
> parameter.  If the language makes the extended precision type available to
> the programmer, this approach allows floating-point calculations to be faster,
> easier, and more understandable than otherwise possible.
>
> The problem arises with code like:
>
>     double d1 = 123456789.0;
>     double d2 = 123456789.1 - d1;
>
> From a semantic standpoint, if the compiler can subtract d1 from
> 123456789.09999999999999999991, yielding 0.09999999999999999991 just as
> cheaply as it can subtract from 123456789.099999994039535522, the former
> is far more likely to be the desired behavior.  That creates a problem,
> though, of what the type of the numeric literal should be.

The type of an unsuffixed floating-point constant is double.  If you
want long double, use an "L" suffix.

> floating-point arguments were passed to printf the same way, as had been
> the case in C as originally designed, the exact type of the value wouldn't
> matter, but the incompatible treatment of "long double" adds many more
> complications.

Adding a third floating-point type added some complications -- not,
IMHO, many of them.

>> Would you want to make it impossible to pass both double and long double
>> arguments to the same variadic function?  For example, this is perfectly
>> legal:
>> 
>>     float f = 1.0;
>>     double d = 2.0;
>>     long double ld = 3.0;
>>     printf("%f %f %Lf\n", f, d, ld);
>
> Nothing wrong with that, but this should be legal *AS WELL*
>
>      printf("%f %f %f\n", f, d, ld);
>
> If a library's ABI for printf requires that it receive floating-point values
> in 64-bit format, the long double should be rounded down to that format; if
> the ABI requires 80-bit values, the double should be extended to 80 bits.

So for some implementations, you suggest that I *can't* pass a long
double value to printf without losing precision?  No thanks.

[...]

> On platforms where it's more efficient to work with extended-precision types
> than packed types, the only reason to pass floating-point numbers as 64-bit
> values rather than 80-bit types would be that one has a printf method that
> only knows how to handle the former.  Otherwise, it would be more efficient
> to have printf *only* accept 80-bit types than for it to need to process both
> 64-bit and 80-bit types.
>
>> Certainly some code gets format strings wrong.  I'm skeptical that most
>> code does so.  (I'm not sure how much code even uses long double; double
>> is sufficient for most purposes.)
>> 
>> C99 made "%lf" synonymous with "%f".
>
> Not a whole lot of code uses "long double", but even less gets the format
> string right.

Citation needed.  (No doubt you can produce individual examples; I'm
looking for something more general.)

It's not that difficult to read the documentation.  I'm certainly aware
that a lot of people don't, but I'm not convinced that it's a widespread
problem.

[...]

> Platforms were "double" is the most efficient type for computations should
> make "long double" synonymous with "double" and perform computations in that
> type.  If they offer some slower higher-precision type, that should be a
> separate compiler intrinsic.

Currently, platforms where double is the most efficient floating-point
type can provide a wider long double type with greater precision and
range and slower performance.

[...]

> Someone who has been taught that one is uppercase and one is lowercase will
> remember that, and I understand the historical reasons things are what they
> are, but could you really blame someone who, given the analogy problem:
>
>    (int   ) is to (long int   ) as "%d" is to "%ld"
>    (double) is to (long double) as "%f" is to ?
>
> figures that the answer should be "%lf"?  I've seen plenty of code like that
> written by people whose compilers treated double and long double as synonymous
> but nonetheless recognized the IEEE-intended proper use of "long double" for
> storing intermediate results.

Sure, programmers make mistakes.  I just don't think that the current
specification (which is consistent and unambiguous) is so confusing that
we need to change the language as radically as you suggest.  And I'm
fairly sure your proposed changes would break existing code, so they're
never going to happen.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 09:04 -0700
  Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-06 17:50 +0100
    Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 11:34 -0700
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-06 19:47 +0100
        Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-06 19:53 +0100
          Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 11:57 -0700
      Re: printf format specifier changes "James Harris" <james.harris.1@gmail.com> - 2015-09-06 10:53 +0100
        Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-06 04:21 -0700
          Re: printf format specifier changes "James Harris" <james.harris.1@gmail.com> - 2015-09-07 10:40 +0100
            Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-07 03:07 -0700
              Re: printf format specifier changes "James Harris" <james.harris.1@gmail.com> - 2015-09-07 15:09 +0100
            Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-07 12:03 +0100
              Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-07 12:46 +0100
                Re: printf format specifier changes "James Harris" <james.harris.1@gmail.com> - 2015-09-07 15:24 +0100
              Re: printf format specifier changes "James Harris" <james.harris.1@gmail.com> - 2015-09-07 15:16 +0100
                Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-07 17:09 +0100
                Re: printf format specifier changes "James Harris" <james.harris.1@gmail.com> - 2015-09-07 17:47 +0100
                Re: printf format specifier changes Richard Damon <Richard@Damon-Family.org> - 2015-09-07 13:29 -0400
                Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-08 12:17 +0100
                Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-09-08 10:13 -0500
                Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-08 17:09 +0100
                Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-09-08 10:08 -0700
                Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-08 19:11 +0100
                Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-09-08 12:24 -0700
                Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-09-08 14:56 -0500
                Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-08 22:18 +0100
                Re: printf format specifier changes Ian Collins <ian-news@hotmail.com> - 2015-09-09 09:29 +1200
                Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-08 22:58 +0100
                Re: printf format specifier changes Ian Collins <ian-news@hotmail.com> - 2015-09-09 10:32 +1200
                Re: printf format specifier changes supercat@casperkitty.com - 2015-09-08 15:57 -0700
                Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-09-08 15:12 -0700
                Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-08 23:45 +0100
                Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-09-08 16:36 -0700
                Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-09-08 19:17 -0500
                Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-09 09:56 +0100
                Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-09-09 11:33 -0700
                Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-09-09 20:35 +0100
                Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-09-08 14:51 -0500
                Re: printf format specifier changes David Brown <david.brown@hesbynett.no> - 2015-09-08 22:41 +0200
              Re: printf format specifier changes Richard Damon <Richard@Damon-Family.org> - 2015-09-07 12:03 -0400
          Re: printf format specifier changes glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-09-10 06:33 +0000
            Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-10 02:38 -0700
          Re: printf format specifier changes supercat@casperkitty.com - 2015-09-10 07:01 -0700
            Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-09-10 12:47 -0700
              Re: printf format specifier changes supercat@casperkitty.com - 2015-09-10 14:08 -0700
                Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-09-10 15:17 -0700
                Re: printf format specifier changes supercat@casperkitty.com - 2015-09-10 16:02 -0700
                Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-09-10 17:31 -0700
                Re: printf format specifier changes supercat@casperkitty.com - 2015-09-11 15:43 -0700
                Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-09-11 16:23 -0700
                Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-09-11 23:33 -0500
                Re: printf format specifier changes Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-11 21:44 -0700
                Re: printf format specifier changes supercat@casperkitty.com - 2015-09-12 09:02 -0700
                Re: printf format specifier changes Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-26 15:00 -0700
                Re: printf format specifier changes supercat@casperkitty.com - 2015-09-27 11:21 -0700
                Re: printf format specifier changes Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-29 10:10 -0700
        Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-06 13:41 +0100
          Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-09-07 00:23 -0500
          Re: printf format specifier changes "James Harris" <james.harris.1@gmail.com> - 2015-09-07 11:31 +0100
            Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-07 03:54 -0700
    Re: printf format specifier changes Richard Heathfield <rjh@cpax.org.uk> - 2015-07-06 19:44 +0100
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-06 22:14 +0100
        Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-07-06 22:43 -0500
        Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-06 23:01 -0700
          Re: printf format specifier changes glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-07-07 06:43 +0000
          Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-09 10:52 +0300
            Re: printf format specifier changes Richard Heathfield <rjh@cpax.org.uk> - 2015-07-09 09:26 +0100
              Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-12 12:56 +0300
                Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-12 06:12 -0700
        Re: printf format specifier changes Rosario19 <Ros@invalid.invalid> - 2015-07-07 10:22 +0200
          Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-07 10:10 +0100
      Re: printf format specifier changes Philip Lantz <prl@canterey.us> - 2015-07-07 00:54 -0700
    Re: printf format specifier changes Nobody <nobody@nowhere.invalid> - 2015-07-07 15:08 +0100
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-07 16:12 +0100
        Re: printf format specifier changes raltbos@xs4all.nl (Richard Bos) - 2015-07-09 10:34 +0000
          Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-09 12:25 +0100
            Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-12 13:02 +0300
              Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-12 11:23 +0100
    Re: printf format specifier changes <william@wilbur.25thandClement.com> - 2015-07-07 11:50 -0700
      Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-07 12:14 -0700
    Re: printf format specifier changes Les Cargill <lcargill99@comcast.com> - 2015-07-07 21:52 -0500
      Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-07 20:42 -0700
        Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-09 11:12 +0300
          Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-09 05:03 -0700
            Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 13:10 -0500
          Re: printf format specifier changes James Kuyper <jameskuyper@verizon.net> - 2015-07-09 08:58 -0400
          Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-09 08:10 -0700
            Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-12 12:58 +0300
              Re: printf format specifier changes James Kuyper <jameskuyper@verizon.net> - 2015-07-12 12:57 -0400
              Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-12 11:14 -0700
                Re: printf format specifier changes gazelle@shell.xmission.com (Kenny McCormack) - 2015-07-12 18:26 +0000
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-08 13:09 +0100
      Re: printf format specifier changes James Kuyper <jameskuyper@verizon.net> - 2015-07-08 08:10 -0400
        Re: printf format specifier changes Les Cargill <lcargill99@comcast.com> - 2015-07-08 08:12 -0500
          Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-08 08:38 -0700
          Re: printf format specifier changes James Kuyper <jameskuyper@verizon.net> - 2015-07-08 12:00 -0400
            Re: printf format specifier changes Les Cargill <lcargill99@comcast.com> - 2015-07-08 17:20 -0500
      Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-09 11:08 +0300
    Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-08 20:30 +0300
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-08 18:56 +0100
        Re: printf format specifier changes gordonb.lmwiv@burditt.org (Gordon Burditt) - 2015-07-09 11:09 -0500
          Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 14:05 -0500
      Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-08 11:27 -0700
        Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-08 15:07 -0500
  Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-06 23:39 -0500
    Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-07 10:19 +0100
      Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 07:57 -0500
        Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-07 14:15 +0100
          Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 14:46 +0100
          Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 09:37 -0500
        Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-09 11:24 +0300
          Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 09:55 -0500
            Re: printf format specifier changes <william@wilbur.25thandClement.com> - 2015-07-09 11:53 -0700
              Re: printf format specifier changes <william@wilbur.25thandClement.com> - 2015-07-09 13:48 -0700
              Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 17:01 -0500
    Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 10:59 +0100
      Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 07:48 -0500
        Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 14:30 +0100
          Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 10:05 -0500
            Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 23:16 +0100
              Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 20:29 -0500
              Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-08 00:43 -0700
              Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-07-09 02:29 -0500
                Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-09 10:34 +0100
                Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 09:35 -0500
          Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-07 08:37 -0700
            Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-07 08:45 -0700
            Re: printf format specifier changes supercat@casperkitty.com - 2015-09-07 08:29 -0700
          Re: printf format specifier changes <william@wilbur.25thandClement.com> - 2015-07-07 12:12 -0700
  Re: printf format specifier changes pooja deshpande <namitadeshpande25@gmail.com> - 2015-07-09 09:29 -0700
    Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 09:34 -0700
      Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 09:59 -0700
        Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-09 22:33 +0100
          Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-09 15:17 -0700
            Re: printf format specifier changes glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-07-10 00:30 +0000
              Re: printf format specifier changes Reinhardt Behm <rbehm@hushmail.com> - 2015-07-10 09:17 +0800
      Re: printf format specifier changes David Kleinecke <dkleinecke@gmail.com> - 2015-07-09 10:02 -0700
        Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 10:19 -0700
      Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-09 21:50 +0100
        Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-09 15:05 -0700
          Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 15:09 -0700
          Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-10 00:28 +0100
            Re: printf format specifier changes raltbos@xs4all.nl (Richard Bos) - 2015-07-13 20:39 +0000
        Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 15:07 -0700
          Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-09 23:16 +0100
            Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 15:25 -0700

csiph-web