Ruud
Veteran Member
First some background: for years I'm busy developing my own OS, Pascal compiler and Basic interpreter. These are very slow projects. To handle numbers, I need to be able to handle Floating Point. Converting a string/ASCII to single or double precision is no problem. But the other way around surely is. If I look on Google, all answers I find point to built-in functions and not how it is done on low-level. So I started to create my own method. At the moment I'm using Pascal with the intention to translate it in assembly later on.
For single precision I use raw force: I created a tables with records of nine words filled with the hex value of 1 ... 10E+43 for the integer part and one table for 0.1 ... 10E-20 for the fraction part. OK, the last is too small but I will expand it. That works fine for single precision but double precision that supports values for 10E-308 ... 10E+308 that won't work.
So I looked at other sources of programs to see how they do it. The first problem: to find well documented sources. One of the best I found was the source for BASIC V2 of the Commodore 64. What the C64 does with fractions is quite simple:
- subtract 0.1 from the fraction as many times as possible and you have your first digit.
- multiply the rest with 10.
- subtract 0.1 from the fraction as many times as possible and you have your next digit.
- if the result is not zero got to step 2.
It does the same with the integer part but using the value 1,000,000,000 as starting value.
But this won't work for double precision, I think. I think I would need over sixty words to represent 10E+308 and even more for the fraction part. So the next idea was subtracting the giving number with the Floating Point version of 10E+308. But.... the Floating Point version of 10E+308 is not exactly 10E308, there always will be a difference. Is this difference acceptable?
I first didn't understand why the C64 used the value 1,000,000,000 as its max value until I realized that FP version of 1,000,000,000 is exactly 1,000,000,000. FYI: the C64 uses 40 bits, the single precision version of 1,000,000,000 is not the same and I have to find out yet what the max value is that it can handle.
So my questions:
- Can someone point me to a site that goes deeper in this material and/or gives practical ideas/sources?
- Has anybody other (better) ideas to handle this?
Thank you very much in advance?
For single precision I use raw force: I created a tables with records of nine words filled with the hex value of 1 ... 10E+43 for the integer part and one table for 0.1 ... 10E-20 for the fraction part. OK, the last is too small but I will expand it. That works fine for single precision but double precision that supports values for 10E-308 ... 10E+308 that won't work.
So I looked at other sources of programs to see how they do it. The first problem: to find well documented sources. One of the best I found was the source for BASIC V2 of the Commodore 64. What the C64 does with fractions is quite simple:
- subtract 0.1 from the fraction as many times as possible and you have your first digit.
- multiply the rest with 10.
- subtract 0.1 from the fraction as many times as possible and you have your next digit.
- if the result is not zero got to step 2.
It does the same with the integer part but using the value 1,000,000,000 as starting value.
But this won't work for double precision, I think. I think I would need over sixty words to represent 10E+308 and even more for the fraction part. So the next idea was subtracting the giving number with the Floating Point version of 10E+308. But.... the Floating Point version of 10E+308 is not exactly 10E308, there always will be a difference. Is this difference acceptable?
I first didn't understand why the C64 used the value 1,000,000,000 as its max value until I realized that FP version of 1,000,000,000 is exactly 1,000,000,000. FYI: the C64 uses 40 bits, the single precision version of 1,000,000,000 is not the same and I have to find out yet what the max value is that it can handle.
So my questions:
- Can someone point me to a site that goes deeper in this material and/or gives practical ideas/sources?
- Has anybody other (better) ideas to handle this?
Thank you very much in advance?