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


Groups > comp.lang.c > #138991

Re: A final word on casting.

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: A final word on casting.
Date 2018-12-23 20:12 +0000
Organization A noiseless patient Spider
Message-ID <87k1k0gfz7.fsf@bsb.me.uk> (permalink)
References (4 earlier) <875zvki950.fsf@bsb.me.uk> <fabc96a8-762e-430e-a567-8658f60fe0d5@googlegroups.com> <QdOTD.34006$Ck1.30728@fx18.am4> <87pntsgm97.fsf@bsb.me.uk> <4hRTD.59185$M61.9999@fx19.am4>

Show all headers | View raw


Bart <bc@freeuk.com> writes:

> On 23/12/2018 17:56, Ben Bacarisse wrote:
>> Bart <bc@freeuk.com> writes:
>
>>> How does "1.999999999999..." in source code end up as /exactly/ 2.0?
>>
>> Because the compiler is giving you the closest double to the number you
>> wrote.
>
> You say that so glibly that anyone reading it would just it take at
> face value.

Why shouldn't they?  That's what's happening.  But I see that your "how"
was literal -- I took it more like "how come...".

> What it doesn't explain is how a compiler, presented with a complex
> set of lossy calculations to arrive at that result, and armed only
> with the same 64-bit set of binary floating point operations, actually
> achieves it.

You'd have to check the source code for each compiler, but it's likely
that gcc uses strtod on the numeric pre-processing token text.

> My own C compiler gets this answer, (exactly 2.0 rather than the next
> value below which is, I believe,
> 1.9999999999999997779553950749686919152736663818359375), but that is
> largely by chance. Generally, floating point constants might differ
> from mature compilers like gcc by one least significant bit.

I don't like that way of describing what happens because it's very
unlikely that chance is involved.  What you mean is you didn't
deliberately design for this result.

>> If you are going to do numerical work, you would need to understand
>> what's happening but it's much simpler, for me, to leave you puzzled.
>
> I know what's happening here. What I don't know is the approach needed
> to turn a string of decimal digits into the closest possible ieee
> 64-bit float value.

Ah, I see.  I am not enough of an expert to say much about it, but the
leeway given in the standard suggests that it might be hard to get right
on some hardware.

Numerical analysis was once my thing, but that was 40 years ago, in
FORTRAN and on IBM 360 hardware.  Those machines had base-16 floating
point formats that had all sorts of odd behaviours.

>> am not at all sure you really want to understand.  If I'm wrong, and
>> this is a part of C you'd like to learn, I will gladly explain.
>
> Whatever the explanation, it still doesn't prevent a program like this:
>
>     printf("%d\n",(int)1.999999999);
>     printf("%d\n",(int)1.9999999999);
>     printf("%d\n",(int)1.99999999999);
>     printf("%d\n",(int)1.999999999999);
>     printf("%d\n",(int)1.9999999999999);
>     printf("%d\n",(int)1.99999999999999);
>     printf("%d\n",(int)1.999999999999999);
>     printf("%d\n",(int)1.9999999999999999);
>     printf("%d\n",(int)1.99999999999999999);
>     printf("%d\n",(int)1.999999999999999999);
>     printf("%d\n",(int)1.9999999999999999999);
>
> from producing this output:
>
>     1
>     1
>     1
>     1
>     1
>     1
>     1
>     2
>     2
>     2
>     2

I don't see why you worry about this.  Is it not what you want?

-- 
Ben.

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


Thread

A final word on casting. chanrasjid@gmail.com - 2018-12-23 03:02 -0800
  Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 12:11 +0000
    Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 13:08 +0000
      Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 14:43 +0000
        Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 14:56 +0000
          Re: A final word on casting. chanrasjid@gmail.com - 2018-12-23 07:08 -0800
            Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 15:55 +0000
              Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 16:07 +0000
              Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-23 08:21 -0800
              Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 17:56 +0000
                Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 19:23 +0000
                Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 20:12 +0000
                Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-23 14:06 -0800
                Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 09:34 -0800
                Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-28 20:29 +0000
                Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 15:07 -0800
                Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-28 20:56 -0500
                Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-29 09:22 -0800
                Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2019-01-01 13:27 -0800
                Re: A final word on casting. Bart <bc@freeuk.com> - 2019-01-02 01:07 +0000
                Re: A final word on casting. Bart <bc@freeuk.com> - 2019-01-03 18:55 +0000
                Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2019-01-03 22:27 -0500
                Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-04 16:25 -0500
                Re: A final word on casting. Ike Naar <ike@iceland.freeshell.org> - 2018-12-28 19:10 +0000
                Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-28 12:33 -0800
                Re: A final word on casting. "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-23 14:11 -0800
                Re: A final word on casting. Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-12-24 02:54 -0800
                Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-24 03:24 -0800
                Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-24 03:29 -0800
                Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-24 03:45 -0800
                Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-24 13:44 +0000
                Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-24 08:51 -0500
              Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-23 14:53 -0500
                Re: A final word on casting. Robert Wessel <robertwessel2@yahoo.com> - 2018-12-24 09:39 -0600
                Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-24 13:38 -0500
              Re: A final word on casting. Manfred <noname@add.invalid> - 2018-12-23 21:22 +0100
              Re: A final word on casting. Manfred <noname@add.invalid> - 2018-12-23 21:33 +0100
            Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-23 10:56 -0500
              Re: A final word on casting. Sjouke Burry <burrynulnulfour@ppllaanneett.nnll> - 2018-12-23 17:18 +0100
        Re: A final word on casting. Bart <bc@freeuk.com> - 2018-12-23 20:03 +0000
    Re: A final word on casting. "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-28 14:47 -0500
  Re: A final word on casting. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 12:24 +0000
  Re: A final word on casting. Richard Damon <Richard@Damon-Family.org> - 2018-12-23 07:28 -0500
    Re: A final word on casting. Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 07:50 -0800
  Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-23 04:35 -0800
    Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-23 04:42 -0800
      Re: A final word on casting. fir <profesor.fir@gmail.com> - 2018-12-23 04:52 -0800
  Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-23 10:28 -0500
  Re: A final word on casting. Barry Schwarz <schwarzb@dqel.com> - 2018-12-23 11:39 -0800
    Re: A final word on casting. Ike Naar <ike@iceland.freeshell.org> - 2018-12-24 07:56 +0000
  Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 14:03 -0800
    Re: A final word on casting. Barry Schwarz <schwarzb@dqel.com> - 2018-12-24 14:39 -0800
      Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 14:50 -0800
        Re: A final word on casting. Keith Thompson <kst-u@mib.org> - 2018-12-24 15:34 -0800
      Re: A final word on casting. Keith Thompson <kst-u@mib.org> - 2018-12-24 15:23 -0800
        Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 20:34 -0800
    Re: A final word on casting. Keith Thompson <kst-u@mib.org> - 2018-12-24 14:52 -0800
      Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 20:25 -0800
    Re: A final word on casting. James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-24 17:52 -0500
    Re: A final word on casting. luser droog <luser.droog@gmail.com> - 2018-12-24 21:27 -0800
      Re: A final word on casting. luser droog <luser.droog@gmail.com> - 2018-12-24 21:39 -0800
      Re: A final word on casting. chanrasjid@gmail.com - 2018-12-24 22:36 -0800

csiph-web