www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A floating-point puzzle

reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Here's a puzzle for you floating-point wizards out there. I have to 
translate the following snippet of FORTRAN code to D:

       REAL B,Q,T
C     ------------------------------
C     |*** COMPUTE MACHINE BASE ***|
C     ------------------------------
       T = 1.
10    T = T + T
       IF ( (1.+T)-T .EQ. 1. ) GOTO 10
       B = 0.
20    B = B + 1
       IF ( T+B .EQ. T ) GOTO 20
       IF ( T+2.*B .GT. T+B ) GOTO 30
       B = B + B
30    Q = ALOG(B)
       Q = .5/Q

Of course I could just do a direct translation, but I have a hunch that 
T, B, and Q can be expressed in terms of real.epsilon, real.min and so 
forth. I have no idea how, though. Any ideas?

(I am especially puzzled by the line after l.20. How can this test ever 
be true? Is the fact that the 1 in l.20 is an integer literal significant?)

-Lars
Aug 05 2009
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Lars T. Kyllingstad:
 (I am especially puzzled by the line after l.20. How can this test ever 
 be true? Is the fact that the 1 in l.20 is an integer literal significant?)
Generally double.inf+1 == double.inf Inspecting the run time values inside a direct translation may give you clues. Bye, bearophile
Aug 05 2009
prev sibling parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Lars T. Kyllingstad wrote:
 Here's a puzzle for you floating-point wizards out there. I have to 
 translate the following snippet of FORTRAN code to D:
 
       REAL B,Q,T
 C     ------------------------------
 C     |*** COMPUTE MACHINE BASE ***|
 C     ------------------------------
       T = 1.
 10    T = T + T
       IF ( (1.+T)-T .EQ. 1. ) GOTO 10
       B = 0.
 20    B = B + 1
       IF ( T+B .EQ. T ) GOTO 20
       IF ( T+2.*B .GT. T+B ) GOTO 30
       B = B + B
 30    Q = ALOG(B)
       Q = .5/Q
 
 Of course I could just do a direct translation, but I have a hunch that 
 T, B, and Q can be expressed in terms of real.epsilon, real.min and so 
 forth. I have no idea how, though. Any ideas?
 
 (I am especially puzzled by the line after l.20. How can this test ever 
 be true? Is the fact that the 1 in l.20 is an integer literal significant?)
 
 -Lars
I finally solved the puzzle by digging through ancient scientific papers, as well as some old FORTRAN and ALGOL code, and the solution turned out to be an interesting piece of computer history trivia. After the above code has finished, the variable B contains the radix of the computer's numerical system. Perhaps the comment should have tipped me off, but I had no idea that computers had ever been anything but binary. But apparently, back in the 50s and 60s there were computers that used the decimal and hexadecimal systems as well. Instead of just power on/off, they had 10 or 16 separate voltage levels to differentiate between bit values. I guess I can just drop this part from my code, then. ;) -Lars
Aug 05 2009
next sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Wed, Aug 5, 2009 at 8:07 AM, Lars T.
Kyllingstad<public kyllingen.nospamnet> wrote:
 Lars T. Kyllingstad wrote:
 Here's a puzzle for you floating-point wizards out there. I have to
 translate the following snippet of FORTRAN code to D:

 =A0 =A0 =A0REAL B,Q,T
 C =A0 =A0 ------------------------------
 C =A0 =A0 |*** COMPUTE MACHINE BASE ***|
 C =A0 =A0 ------------------------------
 =A0 =A0 =A0T =3D 1.
 10 =A0 =A0T =3D T + T
 =A0 =A0 =A0IF ( (1.+T)-T .EQ. 1. ) GOTO 10
 =A0 =A0 =A0B =3D 0.
 20 =A0 =A0B =3D B + 1
 =A0 =A0 =A0IF ( T+B .EQ. T ) GOTO 20
 =A0 =A0 =A0IF ( T+2.*B .GT. T+B ) GOTO 30
 =A0 =A0 =A0B =3D B + B
 30 =A0 =A0Q =3D ALOG(B)
 =A0 =A0 =A0Q =3D .5/Q

 Of course I could just do a direct translation, but I have a hunch that =
T,
 B, and Q can be expressed in terms of real.epsilon, real.min and so fort=
h. I
 have no idea how, though. Any ideas?

 (I am especially puzzled by the line after l.20. How can this test ever =
be
 true? Is the fact that the 1 in l.20 is an integer literal significant?)

 -Lars
I finally solved the puzzle by digging through ancient scientific papers,=
as
 well as some old FORTRAN and ALGOL code, and the solution turned out to b=
e
 an interesting piece of computer history trivia.

 After the above code has finished, the variable B contains the radix of t=
he
 computer's numerical system.

 Perhaps the comment should have tipped me off, but I had no idea that
 computers had ever been anything but binary. But apparently, back in the =
50s
 and 60s there were computers that used the decimal and hexadecimal system=
s
 as well. Instead of just power on/off, they had 10 or 16 separate voltage
 levels to differentiate between bit values.

 I guess I can just drop this part from my code, then. ;)
Very interesting! Thanks for sharing that. But I'm not so sure about the 16 separate voltage levels part. I think they were probably binary underneath. Like the BCD (Binary Coded Decimal) system. --bb
Aug 05 2009
prev sibling next sibling parent "Jouko Koski" <joukokoskispam101 netti.fi> writes:
"Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> wrote:

 After the above code has finished, the variable B contains the radix of 
 the computer's numerical system.
 I guess I can just drop this part from my code, then. ;)
Well, there is certain likelihood that decimal floating point will reappear in the future. However, determining the radix may be unnecessary, because decimal representations may get types of their own. Run-time computation of radix is bad anyway. -- Jouko
Aug 05 2009
prev sibling parent reply Don <nospam nospam.com> writes:
Lars T. Kyllingstad wrote:
 Lars T. Kyllingstad wrote:
 Here's a puzzle for you floating-point wizards out there. I have to 
 translate the following snippet of FORTRAN code to D:

       REAL B,Q,T
 C     ------------------------------
 C     |*** COMPUTE MACHINE BASE ***|
 C     ------------------------------
       T = 1.
 10    T = T + T
       IF ( (1.+T)-T .EQ. 1. ) GOTO 10
       B = 0.
 20    B = B + 1
       IF ( T+B .EQ. T ) GOTO 20
       IF ( T+2.*B .GT. T+B ) GOTO 30
       B = B + B
 30    Q = ALOG(B)
       Q = .5/Q

 Of course I could just do a direct translation, but I have a hunch 
 that T, B, and Q can be expressed in terms of real.epsilon, real.min 
 and so forth. I have no idea how, though. Any ideas?

 (I am especially puzzled by the line after l.20. How can this test 
 ever be true? Is the fact that the 1 in l.20 is an integer literal 
 significant?)

 -Lars
I finally solved the puzzle by digging through ancient scientific papers, as well as some old FORTRAN and ALGOL code, and the solution turned out to be an interesting piece of computer history trivia. After the above code has finished, the variable B contains the radix of the computer's numerical system. Perhaps the comment should have tipped me off, but I had no idea that computers had ever been anything but binary. But apparently, back in the 50s and 60s there were computers that used the decimal and hexadecimal systems as well. Instead of just power on/off, they had 10 or 16 separate voltage levels to differentiate between bit values.
Not quite. They just used exponents which were powers of 10 or 16, rather than 2. BTW, T == 1/real.epsilon. I don't know what ALOG does, so I've no idea what Q is.
 I guess I can just drop this part from my code, then. ;)
 
 -Lars
Aug 05 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Wed, Aug 5, 2009 at 10:16 PM, Don<nospam nospam.com> wrote:
 Lars T. Kyllingstad wrote:
 Lars T. Kyllingstad wrote:
 Here's a puzzle for you floating-point wizards out there. I have to
 translate the following snippet of FORTRAN code to D:

 =A0 =A0 =A0REAL B,Q,T
 C =A0 =A0 ------------------------------
 C =A0 =A0 |*** COMPUTE MACHINE BASE ***|
 C =A0 =A0 ------------------------------
 =A0 =A0 =A0T =3D 1.
 10 =A0 =A0T =3D T + T
 =A0 =A0 =A0IF ( (1.+T)-T .EQ. 1. ) GOTO 10
 =A0 =A0 =A0B =3D 0.
 20 =A0 =A0B =3D B + 1
 =A0 =A0 =A0IF ( T+B .EQ. T ) GOTO 20
 =A0 =A0 =A0IF ( T+2.*B .GT. T+B ) GOTO 30
 =A0 =A0 =A0B =3D B + B
 30 =A0 =A0Q =3D ALOG(B)
 =A0 =A0 =A0Q =3D .5/Q

 Of course I could just do a direct translation, but I have a hunch that
 T, B, and Q can be expressed in terms of real.epsilon, real.min and so
 forth. I have no idea how, though. Any ideas?

 (I am especially puzzled by the line after l.20. How can this test ever
 be true? Is the fact that the 1 in l.20 is an integer literal significa=
nt?)
 -Lars
I finally solved the puzzle by digging through ancient scientific papers=
,
 as well as some old FORTRAN and ALGOL code, and the solution turned out =
to
 be an interesting piece of computer history trivia.

 After the above code has finished, the variable B contains the radix of
 the computer's numerical system.

 Perhaps the comment should have tipped me off, but I had no idea that
 computers had ever been anything but binary. But apparently, back in the=
50s
 and 60s there were computers that used the decimal and hexadecimal syste=
ms
 as well. Instead of just power on/off, they had 10 or 16 separate voltag=
e
 levels to differentiate between bit values.
Not quite. They just used exponents which were powers of 10 or 16, rather than 2. BTW, T =3D=3D 1/real.epsilon. I don't know what ALOG does, so I'v=
e no
 idea what Q is.
Apparently ALOG is just an old name for LOG. At least that's what Google tells me.
Aug 05 2009
parent Don <nospam nospam.com> writes:
Jarrett Billingsley wrote:
 On Wed, Aug 5, 2009 at 10:16 PM, Don<nospam nospam.com> wrote:
 Lars T. Kyllingstad wrote:
 Lars T. Kyllingstad wrote:
 Here's a puzzle for you floating-point wizards out there. I have to
 translate the following snippet of FORTRAN code to D:

      REAL B,Q,T
 C     ------------------------------
 C     |*** COMPUTE MACHINE BASE ***|
 C     ------------------------------
      T = 1.
 10    T = T + T
      IF ( (1.+T)-T .EQ. 1. ) GOTO 10
      B = 0.
 20    B = B + 1
      IF ( T+B .EQ. T ) GOTO 20
      IF ( T+2.*B .GT. T+B ) GOTO 30
      B = B + B
 30    Q = ALOG(B)
      Q = .5/Q

 Of course I could just do a direct translation, but I have a hunch that
 T, B, and Q can be expressed in terms of real.epsilon, real.min and so
 forth. I have no idea how, though. Any ideas?

 (I am especially puzzled by the line after l.20. How can this test ever
 be true? Is the fact that the 1 in l.20 is an integer literal significant?)

 -Lars
I finally solved the puzzle by digging through ancient scientific papers, as well as some old FORTRAN and ALGOL code, and the solution turned out to be an interesting piece of computer history trivia. After the above code has finished, the variable B contains the radix of the computer's numerical system. Perhaps the comment should have tipped me off, but I had no idea that computers had ever been anything but binary. But apparently, back in the 50s and 60s there were computers that used the decimal and hexadecimal systems as well. Instead of just power on/off, they had 10 or 16 separate voltage levels to differentiate between bit values.
Not quite. They just used exponents which were powers of 10 or 16, rather than 2. BTW, T == 1/real.epsilon. I don't know what ALOG does, so I've no idea what Q is.
Apparently ALOG is just an old name for LOG. At least that's what Google tells me.
Then Q is 0.5*ln(0.5). Dunno what use that is.
Aug 06 2009