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


Groups > comp.lang.c > #138834 > unrolled thread

storing and retrieving a double may show a difference?

Started bychanrasjid@gmail.com
First post2018-12-20 23:25 -0800
Last post2018-12-23 20:42 +0100
Articles 8 on this page of 48 — 13 participants

Back to article view | Back to comp.lang.c


Contents

  storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-20 23:25 -0800
    Re: storing and retrieving a double may show a difference? fir <profesor.fir@gmail.com> - 2018-12-21 00:56 -0800
      Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-21 07:02 -0800
        Re: storing and retrieving a double may show a difference? Bart <bc@freeuk.com> - 2018-12-21 15:33 +0000
          Re: storing and retrieving a double may show a difference? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-21 11:16 -0500
            Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-21 09:05 -0800
              Re: storing and retrieving a double may show a difference? fir <profesor.fir@gmail.com> - 2018-12-21 09:43 -0800
              Re: storing and retrieving a double may show a difference? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-21 12:57 -0500
              Re: storing and retrieving a double may show a difference? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-21 20:06 +0000
        Re: storing and retrieving a double may show a difference? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-21 17:10 +0000
          Re: storing and retrieving a double may show a difference? fir <profesor.fir@gmail.com> - 2018-12-21 09:17 -0800
            Re: storing and retrieving a double may show a difference? fir <profesor.fir@gmail.com> - 2018-12-21 09:20 -0800
            Re: storing and retrieving a double may show a difference? fir <profesor.fir@gmail.com> - 2018-12-21 09:24 -0800
          Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-21 21:46 -0800
          Re: storing and retrieving a double may show a difference? Manfred <noname@add.invalid> - 2018-12-22 21:41 +0100
            Re: storing and retrieving a double may show a difference? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-22 21:08 +0000
              Re: storing and retrieving a double may show a difference? Manfred <noname@add.invalid> - 2018-12-23 20:34 +0100
                Re: storing and retrieving a double may show a difference? Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 09:14 -0800
            Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-22 19:16 -0800
        Re: storing and retrieving a double may show a difference? fir <profesor.fir@gmail.com> - 2018-12-21 09:12 -0800
        Re: storing and retrieving a double may show a difference? James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-21 09:23 -0800
          Re: storing and retrieving a double may show a difference? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-21 20:03 +0000
            Re: storing and retrieving a double may show a difference? James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-22 00:10 -0500
              Re: storing and retrieving a double may show a difference? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-22 11:06 +0000
    Re: storing and retrieving a double may show a difference? mark.bluemel@gmail.com - 2018-12-21 01:00 -0800
      What type of problem is it? (Was: storing and retrieving a double may show a difference?) gazelle@shell.xmission.com (Kenny McCormack) - 2018-12-21 09:10 +0000
        Re: What type of problem is it? (Was: storing and retrieving a double may show a difference?) mark.bluemel@gmail.com - 2018-12-21 01:33 -0800
    Re: storing and retrieving a double may show a difference? Bart <bc@freeuk.com> - 2018-12-21 10:16 +0000
    Re: storing and retrieving a double may show a difference? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-21 11:25 +0000
    Re: storing and retrieving a double may show a difference? James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-21 08:06 -0500
      Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-21 07:13 -0800
        Re: storing and retrieving a double may show a difference? James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-21 09:10 -0800
      Re: storing and retrieving a double may show a difference? Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 08:16 -0800
    Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-21 08:09 -0800
    Re: storing and retrieving a double may show a difference? "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-22 17:23 -0800
      Re: storing and retrieving a double may show a difference? "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-22 17:58 -0800
    Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-22 19:30 -0800
      Re: storing and retrieving a double may show a difference? Richard Damon <Richard@Damon-Family.org> - 2018-12-22 22:48 -0500
        Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-22 20:10 -0800
          Re: storing and retrieving a double may show a difference? "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-22 20:41 -0800
          Re: storing and retrieving a double may show a difference? Barry Schwarz <schwarzb@dqel.com> - 2018-12-22 22:02 -0800
            Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-23 02:43 -0800
              Re: storing and retrieving a double may show a difference? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 12:38 +0000
                Re: storing and retrieving a double may show a difference? chanrasjid@gmail.com - 2018-12-23 05:12 -0800
                  Re: storing and retrieving a double may show a difference? fir <profesor.fir@gmail.com> - 2018-12-23 05:32 -0800
                    Re: storing and retrieving a double may show a difference? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-12-23 14:55 +0000
              Re: storing and retrieving a double may show a difference? Richard Damon <Richard@Damon-Family.org> - 2018-12-23 07:43 -0500
      Re: storing and retrieving a double may show a difference? Manfred <noname@add.invalid> - 2018-12-23 20:42 +0100

Page 3 of 3 — ← Prev page 1 2 [3]


#138939

FromBarry Schwarz <schwarzb@dqel.com>
Date2018-12-22 22:02 -0800
Message-ID<9u8u1etbu8vkvenav2lr3fjji3cks4e9aq@4ax.com>
In reply to#138935
On Sat, 22 Dec 2018 20:10:35 -0800 (PST), chanrasjid@gmail.com wrote:

>On Sunday, December 23, 2018 at 11:49:00 AM UTC+8, Richard Damon wrote:
>> On 12/22/18 10:30 PM, chanrasjid@gmail.com wrote:
>> > Just a simple C question now.
>> > 
>> > In C, is it a proper programming thing to do a comparison of values for x, a double as: If (x == 0.0) or if (x == 1.0).
>> > 
>> > Ben just mentioned ==, !=, etc are "just" comparisons, so it seems OK to compare floating values. I think (x/x) will evaluate to/compare == 1.0 unless x == 0.
>> > 
>> 
>> Equality comparisons with floating point have the issue that floating
>> point numbers and calculations are really approximations, so the results
>> may not be exactly what you expect.
>> 
>> For instance, x + y - x - y might not be exactly 0. and
>> 1. / 3 * 3 may not be exactly 1.0
>> 
>> Test for floating point equality does work, it will check if the two
>> number are equal, it is just that if some of the numbers are the results
>> of a calculation, the number you get may not be exactly what you think
>> you should have gotten.
>> 
>> The one spot where comparisons can get really unexpected results is when
>> you get NAN values involved, as if x = NAN, then x == x is false.
>
>I think I understand what you mean by "testing for equality works" even for floating values. If x = 1.23 and y = 1.23, then any sane compiler should (must) give x == y as true; my values x and y are exact values. But there is no exact value for 1.0 / 3.0. 

Except that 1.23 is not exact when stored in any binary floating
variable.  (It can be exact when stored in decimal floating variable
such as on an IBM mainframe.)

Furthermore, if your code consists of
    x = 1.23;
    y = .123*10;
    z = 123/100.;
it would not be surprising for no two to compare equal.

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#138943

Fromchanrasjid@gmail.com
Date2018-12-23 02:43 -0800
Message-ID<e69a2fe3-2707-4959-b5b0-596dc79c0da4@googlegroups.com>
In reply to#138939
On Sunday, December 23, 2018 at 2:02:26 PM UTC+8, Barry Schwarz wrote:
> On Sat, 22 Dec 2018 20:10:35 -0800 (PST), chanrasjid@gmail.com wrote:
> 
> >On Sunday, December 23, 2018 at 11:49:00 AM UTC+8, Richard Damon wrote:
> >> On 12/22/18 10:30 PM, chanrasjid@gmail.com wrote:
> >> > Just a simple C question now.
> >> > 
> >> > In C, is it a proper programming thing to do a comparison of values for x, a double as: If (x == 0.0) or if (x == 1.0).
> >> > 
> >> > Ben just mentioned ==, !=, etc are "just" comparisons, so it seems OK to compare floating values. I think (x/x) will evaluate to/compare == 1.0 unless x == 0.
> >> > 
> >> 
> >> Equality comparisons with floating point have the issue that floating
> >> point numbers and calculations are really approximations, so the results
> >> may not be exactly what you expect.
> >> 
> >> For instance, x + y - x - y might not be exactly 0. and
> >> 1. / 3 * 3 may not be exactly 1.0
> >> 
> >> Test for floating point equality does work, it will check if the two
> >> number are equal, it is just that if some of the numbers are the results
> >> of a calculation, the number you get may not be exactly what you think
> >> you should have gotten.
> >> 
> >> The one spot where comparisons can get really unexpected results is when
> >> you get NAN values involved, as if x = NAN, then x == x is false.
> >
> >I think I understand what you mean by "testing for equality works" even for floating values. If x = 1.23 and y = 1.23, then any sane compiler should (must) give x == y as true; my values x and y are exact values. But there is no exact value for 1.0 / 3.0. 
> 
> Except that 1.23 is not exact when stored in any binary floating
> variable.  (It can be exact when stored in decimal floating variable
> such as on an IBM mainframe.)
> 
> Furthermore, if your code consists of
>     x = 1.23;
>     y = .123*10;
>     z = 123/100.;
> it would not be surprising for no two to compare equal.
> 
> -- 
> Remove del for email

I think yes. But how do you "instinctively" know 1.23 cannot be stored as exact in binary?

[toc] | [prev] | [next] | [standalone]


#138949

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2018-12-23 12:38 +0000
Message-ID<87va3kifk0.fsf@bsb.me.uk>
In reply to#138943
chanrasjid@gmail.com writes:

> On Sunday, December 23, 2018 at 2:02:26 PM UTC+8, Barry Schwarz wrote:
>> On Sat, 22 Dec 2018 20:10:35 -0800 (PST), chanrasjid@gmail.com wrote:
>> 
>> >On Sunday, December 23, 2018 at 11:49:00 AM UTC+8, Richard Damon wrote:
>> >> On 12/22/18 10:30 PM, chanrasjid@gmail.com wrote:
>> >> > Just a simple C question now.
>> >> > 
>> >> > In C, is it a proper programming thing to do a comparison of
>> >> > values for x, a double as: If (x == 0.0) or if (x == 1.0).
>> >> > 
>> >> > Ben just mentioned ==, !=, etc are "just" comparisons, so it
>> >> > seems OK to compare floating values.

Provided you know what you are doing.  The point being made in the post
to which I replied was that you often don't want exact comparison
because floating point calculations usually accrue errors.

<snip>
> I think yes. But how do you "instinctively" know 1.23 cannot be stored
> as exact in binary?

Binary floating point can store integers with a fractional part that is
made up of a sum of halves, quarters, eighths and so on.  So sums of .5,
.25, .125 and so on, but not 1.23.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#138955

Fromchanrasjid@gmail.com
Date2018-12-23 05:12 -0800
Message-ID<0defe12d-f00e-4907-8ead-78402a1d8c27@googlegroups.com>
In reply to#138949
On Sunday, December 23, 2018 at 8:38:31 PM UTC+8, Ben Bacarisse wrote:
> chanrasjid@gmail.com writes:
> 
> > On Sunday, December 23, 2018 at 2:02:26 PM UTC+8, Barry Schwarz wrote:
> >> On Sat, 22 Dec 2018 20:10:35 -0800 (PST), chanrasjid@gmail.com wrote:
> >> 
> >> >On Sunday, December 23, 2018 at 11:49:00 AM UTC+8, Richard Damon wrote:
> >> >> On 12/22/18 10:30 PM, chanrasjid@gmail.com wrote:
> >> >> > Just a simple C question now.
> >> >> > 
> >> >> > In C, is it a proper programming thing to do a comparison of
> >> >> > values for x, a double as: If (x == 0.0) or if (x == 1.0).
> >> >> > 
> >> >> > Ben just mentioned ==, !=, etc are "just" comparisons, so it
> >> >> > seems OK to compare floating values.
> 
> Provided you know what you are doing.  The point being made in the post
> to which I replied was that you often don't want exact comparison
> because floating point calculations usually accrue errors.
> 
> <snip>
> > I think yes. But how do you "instinctively" know 1.23 cannot be stored
> > as exact in binary?
> 
> Binary floating point can store integers with a fractional part that is
> made up of a sum of halves, quarters, eighths and so on.  So sums of .5,
> .25, .125 and so on, but not 1.23.
> 
> -- 
> Ben.

Yes! I activated my sluggish brain a little and discovered that I could have exact binary of 123/2, 123/4,... 123/128..but not for 123/100. Eureka!
 

[toc] | [prev] | [next] | [standalone]


#138958

Fromfir <profesor.fir@gmail.com>
Date2018-12-23 05:32 -0800
Message-ID<030e6b58-5eb4-4858-8405-c7a4ada45deb@googlegroups.com>
In reply to#138955
W dniu niedziela, 23 grudnia 2018 14:12:55 UTC+1 użytkownik chanr...@gmail.com napisał:
> On Sunday, December 23, 2018 at 8:38:31 PM UTC+8, Ben Bacarisse wrote:
> > chanrasjid@gmail.com writes:
> > 
> > > On Sunday, December 23, 2018 at 2:02:26 PM UTC+8, Barry Schwarz wrote:
> > >> On Sat, 22 Dec 2018 20:10:35 -0800 (PST), chanrasjid@gmail.com wrote:
> > >> 
> > >> >On Sunday, December 23, 2018 at 11:49:00 AM UTC+8, Richard Damon wrote:
> > >> >> On 12/22/18 10:30 PM, chanrasjid@gmail.com wrote:
> > >> >> > Just a simple C question now.
> > >> >> > 
> > >> >> > In C, is it a proper programming thing to do a comparison of
> > >> >> > values for x, a double as: If (x == 0.0) or if (x == 1.0).
> > >> >> > 
> > >> >> > Ben just mentioned ==, !=, etc are "just" comparisons, so it
> > >> >> > seems OK to compare floating values.
> > 
> > Provided you know what you are doing.  The point being made in the post
> > to which I replied was that you often don't want exact comparison
> > because floating point calculations usually accrue errors.
> > 
> > <snip>
> > > I think yes. But how do you "instinctively" know 1.23 cannot be stored
> > > as exact in binary?
> > 
> > Binary floating point can store integers with a fractional part that is
> > made up of a sum of halves, quarters, eighths and so on.  So sums of .5,
> > .25, .125 and so on, but not 1.23.
> > 
> > -- 
> > Ben.
> 
> Yes! I activated my sluggish brain a little and discovered that I could have exact binary of 123/2, 123/4,... 123/128..but not for 123/100. Eureka!

in fact the hardware wendors could theoretically provide some other kinds of floats to use, like some maybe based on 
bcd other based on storing nominator and denominator etc and then programmer could
measure and observe the results which plays best in what situation and use different in a various points of code
(its not said yhat float cant be only 
one, this iie887 or what that was the name, ant that this used is something so good, its rather quite historical stuff)

[toc] | [prev] | [next] | [standalone]


#138962

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2018-12-23 14:55 +0000
Message-ID<87bm5ci97d.fsf@bsb.me.uk>
In reply to#138958
fir <profesor.fir@gmail.com> writes:
<snip>
>> Yes! I activated my sluggish brain a little and discovered that I
>> could have exact binary of 123/2, 123/4,... 123/128..but not for
>> 123/100. Eureka!
>
> in fact the hardware wendors could theoretically provide some other
> kinds of floats to use, like some maybe based on bcd other

Not just theoretically.  Many CPUs have decimal floating point formats,
though they are no always based on BCD.

And it's not just hardware vendors.  Some compilers support decimal
floating point though a library where the hardware does not.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#138951

FromRichard Damon <Richard@Damon-Family.org>
Date2018-12-23 07:43 -0500
Message-ID<KpLTD.25594$VV6.9831@fx10.iad>
In reply to#138943
On 12/23/18 5:43 AM, chanrasjid@gmail.com wrote:
> On Sunday, December 23, 2018 at 2:02:26 PM UTC+8, Barry Schwarz wrote:
> I think yes. But how do you "instinctively" know 1.23 cannot be stored as exact in binary?
> 

One necessary, but not sufficient test is it must end in a 5 (or
fraction be all 0)

A fuller test would be that there needs to be a power of 2 that you can
multiply the number by to get an integer value (smaller than the
precision limit of the mantissa). Since 1.23 = 123/100, just multiplying
that by powers of 2 will never get rid of the 2 factors of 5 in the
denominator, so you never get an integer. Numbers that end in 5 have at
least one factor of 5 in them to cancel one  of the 5's in the
denominator, the value needs as many factors of 5 in it as it has
decimal places, but the tests for higher factors of 5 get harder to
express and recognize, and the last digit test quickly deals with a
large majority of the values. For two or more decimal digits, it is ends
in 25, or 75 (or .50), and the number of possible values grows
exponentially (x2) with each added fractional digit.

[toc] | [prev] | [next] | [standalone]


#138986

FromManfred <noname@add.invalid>
Date2018-12-23 20:42 +0100
Message-ID<pvoof2$1fri$1@gioia.aioe.org>
In reply to#138933
On 12/23/2018 4:30 AM, chanrasjid@gmail.com wrote:
> Just a simple C question now.
> 
> In C, is it a proper programming thing to do a comparison of values for x, a double as: If (x == 0.0) or if (x == 1.0).

This is not only a C issue. When I first learned about numerical 
computation and how to handle finite precision errors, it was in a 
course where Fortran was the main language in use.

> 
> Ben just mentioned ==, !=, etc are "just" comparisons, so it seems OK to compare floating values. I think (x/x) will evaluate to/compare == 1.0 unless x == 0.
> 

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c


csiph-web