Thread: RE: [HACKERS] 123.45 - 123 = 0.45

RE: [HACKERS] 123.45 - 123 = 0.45

From
"Jackson, DeJuan"
Date:
> Yes, someone pointed this out to me in private e-mail, and I wrote a C
> program to confirm it.  I just had never seen such rounding on such
> small non-irrational numbers.
But, the problem is that it is irrational in base 2 (or at least so many
digits that it won't fit).-DEJ


Re: [HACKERS] 123.45 - 123 = 0.45

From
Bruce Momjian
Date:
> > Yes, someone pointed this out to me in private e-mail, and I wrote a C
> > program to confirm it.  I just had never seen such rounding on such
> > small non-irrational numbers.
> But, the problem is that it is irrational in base 2 (or at least so many
> digits that it won't fit).

Ah, I figured that out after I sent the message.

--  Bruce Momjian                        |  http://www.op.net/~candle maillist@candle.pha.pa.us            |  (610)
853-3000+  If your life is a hard drive,     |  830 Blythe Avenue +  Christ can be your backup.        |  Drexel Hill,
Pennsylvania19026
 


Re: [HACKERS] 123.45 - 123 = 0.45

From
Tom Lane
Date:
"Jackson, DeJuan" <djackson@cpsgroup.com> writes:
> But, the problem is that it is irrational in base 2 (or at least so many
> digits that it won't fit).

It's not irrational in any base.  123.45 = 12345/100 is a ratio of
integers, ie, rational.  The problem is that the exact representation
of this number as a base-2 fraction is longer than the 52 or so bits
available in float8 format.  (I think it's an infinite repeating
fraction, in the same way that 1/9 = 0.111111111111111111111111111...
is a repeating fraction in base 10.  But even a terminating fraction
will get rounded off as a float8 if it takes more than 52 bits.)

Irrational numbers are those which are not expressible as the ratio of
any two integers, eg, square root of 2.  (The ancient Greeks became
*very* unhappy when they realized that there were such things.)  A
subcategory is transcendentals, which are not expressible as the
solution of any algebraic equation, eg, pi.

Rational numbers have either terminating or repeating expansions in
any base you care to use; irrationals never do.

Sorry, I'll get off my math-pedant soapbox now.  Just tend to get
annoyed when people misuse technical terms.
        regards, tom lane


Re: [HACKERS] 123.45 - 123 = 0.45

From
Bruce Momjian
Date:
> Rational numbers have either terminating or repeating expansions in
> any base you care to use; irrationals never do.
> 
> Sorry, I'll get off my math-pedant soapbox now.  Just tend to get
> annoyed when people misuse technical terms.

And I was a math minor in college.  I am embarassed at what I have
forgotten.


--  Bruce Momjian                        |  http://www.op.net/~candle maillist@candle.pha.pa.us            |  (610)
853-3000+  If your life is a hard drive,     |  830 Blythe Avenue +  Christ can be your backup.        |  Drexel Hill,
Pennsylvania19026