[comp.lang.c] Is .2 irrational?

garys@bunker.UUCP (Gary M. Samuelson) (01/07/87)

In article <153@piaget.UUCP> jc@piaget.UUCP (John Cornelius, System Manager) writes:
>The value .2 is an irrational fraction in binary.

Imprecise, at best.  The number .2 (also represented as 2/10) is
rational, regardless of the form in which it is expressed.  The
problem is that it is not possible to represent exactly that particular
number in typical floating point formats.  A floating point format could
be designed which allowed exact representation of all decimal fractions,
up to some number of places precision, but I know of no implementation
which does that.  (If there were, how would that affect program
portability?)

>Adding the
>representation of (irrational) .2 to (rational) +3. results in a
>number slightly higher than (rational) +3.0, thereby terminating
>the loop.

Adding .2 to 3. should yield something significantly higher
than 3.0 -- approximately 3.2, in fact.

>Henry Spencer is quite right about using floating point numbers
>as loop indices, it is a _bad_ practice and has been for nearly
>30 years.

Well, you just have to do the comparison right.  I remember being
taught never to test two floating point numbers for equality, but
to take the absolute value of the difference and see if it was
less than some epsilon, which was determined by how precise you
wanted to be (or could be).  In fact, in Basic-plus (RSTS/e) there
was an operator which meant "is approximately" (defined as appearing
the same when printed in the standard format).  Very useful, I
thought.

Gary Samuelson

ron@brl-sem.ARPA (Ron Natalie <ron>) (01/08/87)

In article <1384@bunker.UUCP>, garys@bunker.UUCP (Gary M. Samuelson) writes:
>  floating point format could
> be designed which allowed exact representation of all decimal fractions,
> up to some number of places precision.

Gee, how do you deal with 1/3?  I'm not sure how I do that even on
a decimal computer.  Perhaps you meant that can handle an exact represntation
of all fractions of the form INTEGER/(Power of 10)

-Ron

pinkas@mipos3.UUCP (Israel Pinkas) (01/08/87)

In article <568@brl-sem.ARPA> ron@brl-sem.ARPA (Ron Natalie <ron>) writes:
>Gee, how do you deal with 1/3?  I'm not sure how I do that even on
>a decimal computer.  Perhaps you meant that can handle an exact represntation
>of all fractions of the form INTEGER/(Power of 10)

People, I think that you are forgetting the definition of the term
rational.  Rational numbers are defined to be numbers which can be
represented as a fraction of two integers.  Thus, 1/3, 22/7, and 42 are all
rational numbers, whereas e, pi, and i, are not.

What the original poster was asking, I think, is whether it is possible to
represent the number 0.2 (decimal) in binary with a fixed (read limited)
number of digits without losing accuracy.  The answer is no.  The reason is
that 0.2 is really 1/5, and 5 is not a divisor of any power of two.  (5 is
a divisor of a power of 10, 10^1, thus 1/5 can be represented in decimal
with only 1 decimal place.)

-Israel
-- 
----------------------------------------------------------------------
UUCP:	{amdcad,decwrl,hplabs,oliveb,pur-ee,qantel}!intelca!mipos3!pinkas
ARPA:	pinkas%mipos3.intel.com@relay.cs.net
CSNET:	pinkas%mipos3.intel.com

garys@bunker.UUCP (Gary M. Samuelson) (01/08/87)

In article <568@brl-sem.ARPA> ron@brl-sem.ARPA (Ron Natalie <ron>) writes:
>In article <1384@bunker.UUCP>, garys@bunker.UUCP (Gary M. Samuelson) writes:
>> A floating point format could
>> be designed which allowed exact representation of all decimal fractions,
>> up to some number of places precision.
>
>Gee, how do you deal with 1/3?  I'm not sure how I do that even on
>a decimal computer.  Perhaps you meant that can handle an exact represntation
>of all fractions of the form INTEGER/(Power of 10)

I did say "up to some number of places precision," which I think covers
your objection to my imprecise wording.

But if you really want:  Define a floating point number as a triple of
integers (Numerator, Denominator, Exponent).  Let this represent the
number (N/D) * B ** E, where B is a constant (e.g., 10).  In normalized
form, N and D have no common factors, and neither N nor D is divisible by B.
As a special case, define (0, 0, 0) to represent zero.

Formal analysis of this scheme, including proofs that each representable
number would have a unique normalized form, or that its arithmetic
would be well defined, is left as an exercise to the reader.  (However,
if someone wants to pay my living expenses while I pursue this line of
research, let me know :-).

Gary Samuelson

pes@bath63.ux63.bath.ac.uk (Paul Smee) (01/09/87)

There was a 'language' (focal, I believe it was, and was a bit similar to
early BASIC) on the PDP-9 (never saw it anywhere else, was standard there,
yes Virginia I am that old) which stored all numbers as fractions -- that
is, each number used 2 storage locations, one for the numerator and one
for the denominator.  So, subject to storage-unit length, any rational
number within range could be represented exactly.

There were some obvious difficulties, of course, such as the fact that the
maximum magnitude which could be represented depended on the denominator
of the fraction.  Still, it offers an approach...

sierchio@milano.UUCP (01/09/87)

e and pi are not only irrational, they are transcendental.  


-- 
	
	Michael Sierchio @ MCC Software Technology Program

	UUCP:	ut-sally!im4u!milano!sierchio
	ARPA:	sierchio@mcc.ARPA

	"WE REVERSE THE RIGHT TO SERVE REFUSE TO ANYONE"

KJBSF%SLACVM.BITNET@wiscvm.wisc.edu (01/14/87)

Date: 14 January 1987, 09:28:14 PST
From: Kevin J. Burnett          x3330                <KJBSF@SLACVM>
To:   <INFO-C@BRL-SMOKE.ARPA>
Subject: Re: Re: Is .2 irrational?

<ron@BRL-SEM.ARPA> writes:
>>  floating point format could
>> be designed which allowed exact representation of all decimal fractions,
>> up to some number of places precision.
>
>Gee, how do you deal with 1/3?  I'm not sure how I do that even on
>a decimal computer.  Perhaps you meant that can handle an exact represntation
>of all fractions of the form INTEGER/(Power of 10)
I would think that 'up to some number of places precision' should have meant
something to you.  The phrase would suggest to me that the author you are
quoting meant that, say, up to 5 decimal places, 1/3 = .33333 .

stever@videovax.Tek.COM (Steven E. Rice, P.E.) (01/15/87)

In article <1384@bunker.UUCP>, Gary M. Samuelson (garys@bunker.UUCP)
writes:

> . . .  The
> problem is that it is not possible to represent exactly that particular
> number in typical floating point formats.  A floating point format could
> be designed which allowed exact representation of all decimal fractions,
> up to some number of places precision, but I know of no implementation
> which does that.  . . .

Oh, how quickly the glory fades!  The IBM 1620 does (now mostly _did_)
exactly what you wanted, since it is (was) a variable-operand-length, BCD
machine.

					Steve Rice

----------------------------------------------------------------------------
{decvax | hplabs | ihnp4 | uw-beaver}!tektronix!videovax!stever

inews > 50% filler
  inews > 50% filler
    inews > 50% filler
      inews > 50% filler
        inews > 50% filler

billw@navajo.UUCP (01/16/87)

I was under the impression that many early microcomputer versions
of basic use a floating point format that used a BCD mantisa and
a binary exponent whose base was 10.  I suspect that some still
do, given that most micros can add and subtract BCD (and can't
multiply even binary) anyway.

IBM360 floating point used 16 as the base of the exponent...

BillW