--- title: "Decimal to Floating Point Binary" type: "article" slug: "decimal-to-floating-point-binary" url: "http://localhost/article/decimal-to-floating-point-binary/" markdown_url: "http://localhost/article/decimal-to-floating-point-binary.md" published_at: "2020-10-27T17:11:36+00:00" modified_at: "2026-11-10T10:17:26+00:00" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "Use built-in floating ROM routine to convert decimal number to floating point format." post_tag: - name: "1983" slug: "year-1983" taxonomy: "post_tag" url: "http://localhost/tag/year-1983/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" - name: "Type-in program" slug: "type-in-program" taxonomy: "post_tag" url: "http://localhost/tag/type-in-program/" - name: "ZX81" slug: "zx81" taxonomy: "post_tag" url: "http://localhost/tag/zx81/" model: - name: "Sinclair ZX81" slug: "zx81" taxonomy: "model" url: "http://localhost/model/zx81/" - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" indiv: - name: "C. C. Stalder" slug: "c-c-stalder" taxonomy: "indiv" url: "http://localhost/indiv/c-c-stalder/" publication: "Syntax" publication_r: id: 10246 title: "Syntax" type: "periodical" url: "http://localhost/periodical/syntax/" authors: "C. C. Stalder" volume: "4" issue: "5" issues_articles: - id: 26715 title: "Syntax/Syntax ZX80 v4 n5" type: "issue" url: "http://localhost/issue/syntax-syntax-zx80-v4-n5/" pages: "8-9" pubdate: "May 1983" archive_link: false volumeissue: "v4n5" --- # Decimal to Floating Point Binary [John Bowden](http://localhost/indiv/bowden-john/) describes an interesting pencil-and-paper procedure to convert decimal numbers to ZX/TS floating point binary (Jan.83). His procedure works fine, but is slow and can only handle positive integers smaller than 2**32. Your ZX/TS ROM has a number conversion routine that is faster, not as limited and easy to use. You can even use it in command mode: ``` LET X0X=N LET Y0Y=PEEK 16404+256*PEEK 16405 PRINT PEEK (Y0Y-6),,PEEK (Y0Y-5),,PEEK (Y0Y-4),,PEEK (Y0Y-3),,PEEK (Y0Y-2) ``` These three commands display a list of decimal values of the five bytes for number n, in Sinclair-type floating point format. But this routine requires a lot of typing to convert one number. It works by assigning a decimal number to the variable X0X, the last item stored in the variables area of program memory. The last-defined variable is always located just before the address held in system variable E_LINE. The second command PEEKs E_LINE to get the address. The third command backs up from that address and displays bytes containing the floating point number, exponent byte first, followed by the mantissa (decimal part of a logarithm) bytes in order of decreasing significance. Yet, a BASIC program modeled after the preceding command sequence gives problems–primarily insuring X0X is always the last-defined variable. Avoid this problem by using the variables *start* area marked by system variable VARS, rather than the variables *end* area marked by E_LINE. Listing 1 shows 11 lines you can use to start any program needing decimal to binary conversion. To use this program assign the decimal number (or numeric expression) to variable X0X, and then GOSUB 50. When executing the program by RUN or GOTO, line 10 clears the variables area of program memory. Thus, variable X0X initially gets the value 0 and becomes the first-defined variable. System variable VARS always holds variable X0X’s address. On executing a program line that assigns a value to a variable, the computer automatically converts the value to floating point binary form and stores it that way. Line 50 gets the X0X address from VARS. Lines 60-68 put each byte of the binary number into its own variable. After the subroutine returns, the main program can do anything it likes with the information, held in variables Z1Z to Z5Z. This conversion routine uses unique variable names to minimize inadvertent re-use of the same names later in the main program. Reusing variable names won’t bother the conversion routine, but the main program might go berserk. This routine is reasonably fast, and can convert any number–positive, negative, fractional, or any ZX/TS numeric expression. ### Listing 1. ``` 10 CLEAR 20 LET X0X=0 30 GOTO 80 40 REM SUBROUTINE TO CONVERT DECIMAL NUMBER IN X0X TO FIVE BYTE F.P. BINARY IN Z1Z TO Z5Z 50 LET Y0Y=PEEK 16400+256*PEEK 16401 60 LET Z1Z=PEEK (Y0Y+3) 62 LET Z2Z=PEEK (Y0Y+4) 64 LET Z3Z=PEEK (Y0Y+5) 66 LET Z4Z=PEEK (Y0Y+6) 68 LET Z5Z=PEEK (Y0Y+7) 70 RETURN 80 REM CONTINUE FROM HERE WITH MAIN PROGRAM SYNTACTIC SUM: 17300, 8K ROM ```