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


Groups > comp.misc > #2678

Re: BASIC is 50

From "James Harris" <james.harris.1@gmail.com>
Newsgroups comp.misc
Subject Re: BASIC is 50
Date 2014-05-07 19:09 +0100
Organization A noiseless patient Spider
Message-ID <lkdss3$vi1$1@dont-email.me> (permalink)
References (4 earlier) <gknav.371282$sa2.196137@fx33.am4> <bsuj7gFd631U1@mid.individual.net> <Xtpav.335997$5V5.276603@fx08.am4> <lkdi9g$cpd$1@dont-email.me> <8duav.279147$Tp4.228677@fx11.am4>

Show all headers | View raw


"BartC" <bc@freeuk.com> wrote in message 
news:8duav.279147$Tp4.228677@fx11.am4...
>
>
> "James Harris" <james.harris.1@gmail.com> wrote in message
> news:lkdi9g$cpd$1@dont-email.me...
>> "BartC" <bc@freeuk.com> wrote in message
>> news:Xtpav.335997$5V5.276603@fx08.am4...
>
>>> (In any case, typical 64-bit floating point can represent amounts up to
>>> nearly $100 trillion, expressed in whole cents.)
>>
>> The big problem with floating point is subtraction. All programmers using
>> floats should be aware of it but I don't know if it is generally taught.
>> Even with 64-bit floats if you have two intermediate results that are
>> similar and you subtract one from the other the result may have lost lots
>> of digits of precision.
>>
>> For example, if you have a precise but very small floating point number X
>> such as 3.1415926539e-10 and the code does
>>
>>  X = X + 1
>>  X = X - 1
>>
>> The code looks harmless but X will have lost a lot of its precision. It
>> may now be only something like 3.1e-10.
>
> I think the precision will be lost as soon as you do X=X+1.

Yes, although the precision of the floating point value of X is really 
unchanged at that point. Its precision is still the same number of digits 
that it was before. For the sake of choosing some numbers say it has 11 
digits of precision both before and after the addition of 1. If 1 is not 
subsequently subtracted the value of X is still precise to 11 digits and 
fine as a highly precise floating point number. The lost information is now 
around the 20 digits mark which is almost always negligible. Still, on 
subtraction the result loses precision which is significant.

> Although you're
> right in that someone who expects X to be unchanged after those two
> assignments may not be aware of the problem. (It's like right-shifting an 
> integer by N places followed by left-shifting by N places.)
>
>> For financial applications decimal numbers or scaled integers are 
>> normally
>> safer.
>
> But it's possible (depending on the number of decimals) they won't be able
> to represent 3.14159265359e-10 anyway. Maybe only 3.1e-10 ! Or 0.

Yes. I was just trying to illustrate the problem with simple numbers. As you 
know, the issue would arise in the same way if the number of pennies in a 
person's account was 3 million and as a result of an intermediate 
calculation 10e18 was added and then a number close to 10e18 was subtracted. 
Mathematically the subtraction may be correct but the result in a computer 
would differ markedly.

Don't get me wrong, I believe that floats can be used for finances as long 
as the programmer is extremely careful about intermediate values and does 
lots of boundary testing. But even then the code can become very hard to 
maintain safely.

(I was prompted to reply because I saw the comment that 64-bit floats can 
represent up to $100 trillion. That may be true for static storage but they 
would be very difficult to do calculations on due to the danger of losing 
information.)

James

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


Thread

BASIC is 50 Gordon Henderson <gordon+usenet@drogon.net> - 2014-04-30 09:34 +0000
  Re: BASIC is 50 Ant <ant@zimage.comANT> - 2014-04-30 03:42 -0700
    Re: BASIC is 50 Bob Eager <news0002@eager.cx> - 2014-04-30 10:46 +0000
      Re: BASIC is 50 Gordon Henderson <gordon+usenet@drogon.net> - 2014-04-30 12:03 +0000
  Re: BASIC is 50 Rich <rich@example.com> - 2014-04-30 11:12 +0000
    Re: BASIC is 50 scott@alfter.diespammersdie.us (Scott Alfter) - 2014-04-30 15:56 +0000
      Re: BASIC is 50 Rich <rich@example.com> - 2014-04-30 20:41 +0000
        Re: BASIC is 50 Will Honea <whonea@whonea.net> - 2014-04-30 18:38 -0600
          Re: BASIC is 50 Rich <rich@example.com> - 2014-05-01 02:38 +0000
            Re: BASIC is 50 Will Honea <whonea@whonea.net> - 2014-04-30 22:50 -0600
      Re: BASIC is 50 Rich <rich@example.com> - 2014-04-30 20:48 +0000
        Re: BASIC is 50 Richard Kettlewell <rjk@greenend.org.uk> - 2014-05-02 09:27 +0100
          Re: BASIC is 50 Tim Streater <timstreater@greenbee.net> - 2014-05-02 09:50 +0100
        Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-07 10:48 +0100
          Re: BASIC is 50 Tim Streater <timstreater@greenbee.net> - 2014-05-07 10:57 +0100
          Re: BASIC is 50 Huge <Huge@nowhere.much.invalid> - 2014-05-07 11:20 +0000
            Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-07 13:14 +0100
              Re: BASIC is 50 Marko Rauhamaa <marko@pacujo.net> - 2014-05-07 16:05 +0300
                Re: BASIC is 50 Paul Sture <nospam@sture.ch> - 2014-05-08 18:06 +0200
                Re: BASIC is 50 Marko Rauhamaa <marko@pacujo.net> - 2014-05-08 20:17 +0300
              Re: BASIC is 50 "James Harris" <james.harris.1@gmail.com> - 2014-05-07 16:08 +0100
                Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-07 18:38 +0100
                Re: BASIC is 50 "James Harris" <james.harris.1@gmail.com> - 2014-05-07 19:09 +0100
                Re: BASIC is 50 Huge <Huge@nowhere.much.invalid> - 2014-05-07 19:08 +0000
              Re: BASIC is 50 greymausg@mail.com - 2014-05-08 10:15 +0000
                Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-08 11:38 +0100
              Re: BASIC is 50 Paul Sture <nospam@sture.ch> - 2014-05-08 17:55 +0200
          Re: BASIC is 50 Paul Sture <nospam@sture.ch> - 2014-05-08 17:44 +0200
          Re: BASIC is 50 Rich <rich@example.com> - 2014-05-12 12:39 +0000
            Re: BASIC is 50 Marko Rauhamaa <marko@pacujo.net> - 2014-05-12 16:06 +0300
            Re: BASIC is 50 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-12 22:48 +0100
              Re: BASIC is 50 Rich <rich@example.com> - 2014-05-13 12:03 +0000
                Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-13 13:23 +0100
                Re: BASIC is 50 Rich <rich@example.com> - 2014-05-13 16:22 +0000
                Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-13 19:42 +0100
                Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-13 19:49 +0100
                Re: BASIC is 50 Rich <rich@example.com> - 2014-05-14 01:09 +0000
                Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-14 09:51 +0100
                Re: BASIC is 50 bde@besplex.bde.org (Bruce Evans) - 2014-05-14 10:36 +0000
                Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-14 12:58 +0100
                Re: BASIC is 50 bde@besplex.bde.org (Bruce Evans) - 2014-05-14 16:13 +0000
                Re: BASIC is 50 "BartC" <bc@freeuk.com> - 2014-05-14 19:24 +0100
                Re: BASIC is 50 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-13 15:50 +0100
    Re: BASIC is 50 Johny B Good <johnny-b-good@invalid.ntlworld.com> - 2014-05-01 02:50 +0100
    Re: BASIC is 50 David Hume <David.Hume@example.com> - 2014-05-01 09:06 +0100
      Re: BASIC is 50 Gordon Henderson <gordon+usenet@drogon.net> - 2014-05-01 09:56 +0000
      Re: BASIC is 50 Rich <rich@example.com> - 2014-05-01 10:25 +0000
        Re: BASIC is 50 Marko Rauhamaa <marko@pacujo.net> - 2014-05-01 16:43 +0300
          Re: BASIC is 50 Gordon Henderson <gordon+usenet@drogon.net> - 2014-05-01 18:03 +0000
          Re: BASIC is 50 Bob Eager <news0002@eager.cx> - 2014-05-01 21:35 +0000
        Re: BASIC is 50 Rob <monst.oorzudo@ntlworld.com> - 2014-05-01 22:41 +0100
          Re: BASIC is 50 David Hume <David.Hume@example.com> - 2014-05-02 09:26 +0100
            Re: BASIC is 50 Hils <hils@saynotospam.net> - 2014-05-02 10:46 +0100
            Re: BASIC is 50 Johny B Good <johnny-b-good@invalid.ntlworld.com> - 2014-05-02 23:11 +0100
  Re: BASIC is 50 Anssi Saari <as@sci.fi> - 2014-04-30 17:51 +0300
  Re: BASIC is 50 Peter Percival <peterxpercival@hotmail.com> - 2014-05-14 17:40 +0100

csiph-web