Author Topic: Floating point to ASCII  (Read 177 times)

JK

  • Member
  • **
  • Posts: 102
Floating point to ASCII
« on: April 21, 2021, 01:47:37 AM »
I wanted to have code for converting different floating point numbers to ASCII-text, which is independent of the CRT. There is code (fptoa.asm) by Tim Roberts, which is has been part of the MASM32 package for a long time, so i suppose it´s correct and bug free.

This code is written for real8 conversion, but i wanted to be able to convert real4, real10 and real16 too. All of these floating point type have different ranges and a different number of valid digits (digits of precision). So it would be nice to present as output only as much digits, as there are valid digits per format. That is, writing more than 6(7) digits for a real4 is pointless, because in a mathematical sense only 6 decimal places are always guaranteed to be valid, even if the conversion process may produce more digits to write.   

For real8 the code is basically the same as proposed by Tim Roberts, i made adaptions for real4 and real10. It seems to work, but then i found a number causing problems when converted as a real4. I´m not absolutely sure, if the fix i found (change the floor value from 1.0e6 to 999999.1) doesn´t introduce other (rounding) problems.

I have no clue how to convert (losslessly) real16, because these don´t fit into the FPU registers.

There is also code for comparing conversion speed of these three routines:
- Real4_2_ASCII (by Siemanski)
- _StrToFloat (updated by me)
- _snprintf (CRT)


Question:
- is _StrToFloat working correctly, or are there still problems in code or the mathematical implementation?
- how could i do this (lossless) for real16 floating point numbers?


Thanks


JK   

JK

  • Member
  • **
  • Posts: 102
Re: Floating point to ASCII
« Reply #1 on: May 03, 2021, 03:13:44 AM »
Next version (with bug fixes): assembles with ASMC and runs in 32 and 64 bit.

32 bit:
Code: [Select]
asmc.exe /Fl /c /coff /Cp /I...
LINK.EXE /SUBSYSTEM:CONSOLE /RELEASE /VERSION:4.0 /LIBPATH:"...

64 bit:
Code: [Select]
asmc.exe /Fl /c /Zp16 /win64 /Cp /W0 /I"...
LINK.EXE /SUBSYSTEM:CONSOLE /RELEASE /VERSION:4.0 /MACHINE:X64 /LIBPATH:"...

I ran exhaustive tests for REAL4 and non-exhaustive tests for REAL8 and REAL10, see comments in code and below. So i´m quite sure this works correctly for REAL4, i´m not absolutely sure, that REAL8 and REAL10 are always correct.

My approach for REAL4 was "brute force": i created a string for every possible number in REAL4 range, converted it to a REAL4 number and then back to a string. Finally i compared both strings. So for REAL4 i can say: what goes in comes out.

Running such a test takes half an hour on my machine, so doing this for REAL8 range is impossible, but what i did for REAL8 and REAL10 was: testing all possible numbers for these near a power of ten. Such numbers turned out to be problematic in REAL4 range (a problem with representation in binary and decimal format). 

Nevertheless it would be of help, if someone else tested these two conversion algos with his own ideas and methods.


Thanks


JK