Author Topic: Converting real8 to string  (Read 16784 times)

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Converting real8 to string
« on: April 30, 2013, 04:07:27 AM »
Hi
    Here we have the ASM procedures

            ConvertFloat8DS and ConvertFloat8DSD

   to convert a real8 (qword) to a string,

    the second to specify the number of decimal places
    we want. These converters doesnt use BCD. It gets
    the qword integer and converts it to a string.

    All is in the file ConverterDQ.inc and
   
    we can test using TestCnvDQ12_DS.asm.

    All these files are in ConverterDQ folder.

EDIT: i replaced ConverterDQ by Converter8

    Here we have the new ASM procedures

        ConvertFloat8DR and ConvertFloat8DRD
        ConvertReal8DR  and ConvertReal8DRD

    and the last

        ConvertFloat8DS and ConvertFloat8DSD
        ConvertReal8DS  and ConvertReal8DSD

    . Float version use length behind the address
    . Real     "    doesnt use

    . Real version only: we may specify string address = 0
      The proc returns the address in EDX

        invoke  ConvertReal8DRD, addr _qword, 0, 14
        print   edx, 13,10

    To use it

        include     PowerTable.inc
        include     Convert8DR.inc
        include     Convert8DRD.inc
        include     Convert8DS.inc
        include     Convert8DSD.inc
       
        uncomment ;$DECIMALPLACES_REAL8  equ 15
       
   
    Test yourself:

            TestCycles8_10D.asm
            TestCycles8_15D.asm
            TestTime8_10D.asm
            TestTime8_15D.asm
           
            TestFloat8DR_10D.asm
            TestReal8DR_10D.asm           
            ...

    All these files are in Converter8 folder.

EDIT: the old Converter8 had a bug


EDITuse the last Converter8 because the first has a bug in the procedures
           ConvertReal8DR, ...Real8DRD, ..., Real8DS and ...Real8DSD.
           It sets the length if ERROR.
« Last Edit: May 02, 2018, 01:28:44 AM by RuiLoureiro »

dedndave

  • Member
  • *****
  • Posts: 8808
  • Still using Abacus 2.0
    • DednDave
Re: Converting real8 to string
« Reply #1 on: April 30, 2013, 08:21:04 AM »
looking good, Rui   :t

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Converting real8 to string
« Reply #2 on: April 30, 2013, 10:25:08 PM »
Well done. :t

Gunther
Get your facts first, and then you can distort them.

ToutEnMasm

  • Member
  • *****
  • Posts: 1190
    • EditMasm
Re: Converting real8 to string
« Reply #3 on: April 30, 2013, 10:36:06 PM »

I try a question,is it better than the FloatToBCD (fptoa.asm) ?
Or perhaps better than the fcvt for the crt ?
 
Fa is a musical note to play with CL

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Converting real8 to string
« Reply #4 on: May 01, 2013, 02:40:28 AM »

I try a question,is it better than the FloatToBCD (fptoa.asm) ?
Or perhaps better than the fcvt for the crt ?
               I think it is better but i will try to show all the numbers i have.
               now i improved it again, please wait  :t

For now i have this:
(CNV1 convert EDX:EAX using div, CNV2 uses magic numbers)
Code: [Select]
with CNV1

502  cycles, ConvertFloat8DSD, direct, fxam, fyl2x, ebp - 15 digits
554  cycles, ConvertFloat8DYD, direct, examine, fxtract, esp - 15 digits
563  cycles, ConvertFloat8DRD, direct, examine, fxtract, ebp - 15 digits
576  cycles, ConvertFloat8DXD, direct, fxam, fxtract, esp - 15 digits
683  cycles, ConvertFloat8DFD, direct, examine, fyl2x, ebp - 15 digits
754  cycles, ConvertFloat8BX, BCD, fxam, fxtract, ebp - 15 digits
757  cycles, ConvertFloat8BY, BCD, examine, fxtract, esp - 15 digits
763  cycles, ConvertFloat8BF, BCD, fxam, fxtract, esp - 15 digits
772  cycles, ConvertFloat8CT, BCD-CT, fxam, fxtract, esp - 15 digits
889  cycles, ConvertFloat8CTD, BCD-CT, fxam, fxtract, esp - 15 digits
989  cycles, ConvertFloat8BFD, BCD, fxam, fxtract, esp - 15 digits
992  cycles, ConvertFloat8BYD, BCD, examine, fxtract, esp - 15 digits
1000  cycles, ConvertFloat8DX, direct, fxam, fxtract, esp - 15 digits
1002  cycles, ConvertFloat8DY, direct, examine, fxtract, esp - 15 digits
1010  cycles, ConvertFloat8DR, direct, examine, fxtract, ebp - 15 digits
1028  cycles, ConvertFloat8BXD, BCD, fxam, fxtract, ebp - 15 digits
1056  cycles, ConvertFloat8ZX, BCD - old - 15 digits
1068  cycles, ConvertFloat8Z, BCD -old - 15 digits
1124  cycles, ConvertFloat8DS, direct, fxam, fyl2x, ebp - 15 digits
1153  cycles, ConvertFloat8DF, direct, examine, fyl2x, ebp - 15 digits
2728  cycles, ConvertFloat8DWD, direct,Save FPU, fxam, fxtract, esp -15 digits
2921  cycles, ConvertFloat8, BCD, Save FPU -old - 15 digits
3027  cycles, ConvertFloat8BWD, BCD, Save FPU, fxam, fxtract, ebp - 15 digits
3126  cycles, ConvertFloat8BW, BCD, Save FPU, fxam, fxtract, ebp - 15 digits
3885  cycles, ConvertFloat8DW, direct,Save FPU, fxam, fxtract, esp -15 digits
 ********** END **********

with CNV2

371  cycles, ConvertFloat8DXD, direct, fxam, fxtract, esp - 15 digits
373  cycles, ConvertFloat8DYD, direct, examine, fxtract, esp - 15 digits
509  cycles, ConvertFloat8DSD, direct, fxam, fyl2x, ebp - 15 digits
540  cycles, ConvertFloat8DX, direct, fxam, fxtract, esp - 15 digits
543  cycles, ConvertFloat8DR, direct, examine, fxtract, ebp - 15 digits
549  cycles, ConvertFloat8DY, direct, examine, fxtract, esp - 15 digits
564  cycles, ConvertFloat8DRD, direct, examine, fxtract, ebp - 15 digits
673  cycles, ConvertFloat8DS, direct, fxam, fyl2x, ebp - 15 digits
680  cycles, ConvertFloat8DF, direct, examine, fyl2x, ebp - 15 digits
685  cycles, ConvertFloat8DFD, direct, examine, fyl2x, ebp - 15 digits
756  cycles, ConvertFloat8BX, BCD, fxam, fxtract, ebp - 15 digits
763  cycles, ConvertFloat8BY, BCD, examine, fxtract, esp - 15 digits
764  cycles, ConvertFloat8BYD, BCD, examine, fxtract, esp - 15 digits
767  cycles, ConvertFloat8BF, BCD, fxam, fxtract, esp - 15 digits
781  cycles, ConvertFloat8CT, BCD-CT, fxam, fxtract, esp - 15 digits
794  cycles, ConvertFloat8CTD, BCD-CT, fxam, fxtract, esp - 15 digits
811  cycles, ConvertFloat8BFD, BCD, fxam, fxtract, esp - 15 digits
1008  cycles, ConvertFloat8BXD, BCD, fxam, fxtract, ebp - 15 digits
1053  cycles, ConvertFloat8ZX, BCD - old - 15 digits
1064  cycles, ConvertFloat8Z, BCD -old - 15 digits
2710  cycles, ConvertFloat8DWD, direct,Save FPU, fxam, fxtract, esp -15 digits
2930  cycles, ConvertFloat8, BCD, Save FPU -old - 15 digits
3023  cycles, ConvertFloat8BWD, BCD, Save FPU, fxam, fxtract, ebp - 15 digits
3532  cycles, ConvertFloat8BW, BCD, Save FPU, fxam, fxtract, ebp - 15 digits
3572  cycles, ConvertFloat8DW, direct,Save FPU, fxam, fxtract, esp -15 digits
 ********** END **********
 ; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

with CNV1

326  cycles, ConvertFloat8DXD, direct, fxam, fxtract, esp - 18 digits
327  cycles, ConvertFloat8DYD, direct, examine, fxtract, esp - 18 digits
331  cycles, ConvertFloat8DRD, direct, examine, fxtract, ebp - 18 digits
461  cycles, ConvertFloat8DSD, direct, fxam, fyl2x, ebp - 18 digits
472  cycles, ConvertFloat8DFD, direct, examine, fyl2x, ebp - 18 digits
695  cycles, ConvertFloat8CTD, BCD-CT, fxam, fxtract, esp - 18 digits
702  cycles, ConvertFloat8BY, BCD, examine, fxtract, esp - 18 digits
703  cycles, ConvertFloat8BFD, BCD, fxam, fxtract, esp - 18 digits
705  cycles, ConvertFloat8BYD, BCD, examine, fxtract, esp - 18 digits
711  cycles, ConvertFloat8BXD, BCD, fxam, fxtract, ebp - 18 digits
714  cycles, ConvertFloat8BF, BCD, fxam, fxtract, esp - 18 digits
714  cycles, ConvertFloat8BX, BCD, fxam, fxtract, ebp - 18 digits
724  cycles, ConvertFloat8CT, BCD-CT, fxam, fxtract, esp - 18 digits
1155  cycles, ConvertFloat8ZX, BCD - old - 18 digits
1161  cycles, ConvertFloat8Z, BCD -old - 18 digits
1310  cycles, ConvertFloat8DR, direct, examine, fxtract, ebp - 18 digits
1318  cycles, ConvertFloat8DY, direct, examine, fxtract, esp - 18 digits
1334  cycles, ConvertFloat8DX, direct, fxam, fxtract, esp - 18 digits
1429  cycles, ConvertFloat8DS, direct, fxam, fyl2x, ebp - 18 digits
1434  cycles, ConvertFloat8DF, direct, examine, fyl2x, ebp - 18 digits
2585  cycles, ConvertFloat8DWD, direct,Save FPU, fxam, fxtract, esp -18 digits
2913  cycles, ConvertFloat8BWD, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
3034  cycles, ConvertFloat8, BCD, Save FPU -old - 18 digits
3041  cycles, ConvertFloat8BW, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
3713  cycles, ConvertFloat8DW, direct,Save FPU, fxam, fxtract, esp -18 digits
 ********** END **********
; ----------------------------------------------------------------------------------------------
With CNV2

325  cycles, ConvertFloat8DYD, direct, examine, fxtract, esp - 18 digits
327  cycles, ConvertFloat8DXD, direct, fxam, fxtract, esp - 18 digits
336  cycles, ConvertFloat8DRD, direct, examine, fxtract, ebp - 18 digits
452  cycles, ConvertFloat8DFD, direct, examine, fyl2x, ebp - 18 digits
454  cycles, ConvertFloat8DSD, direct, fxam, fyl2x, ebp - 18 digits
559  cycles, ConvertFloat8DX, direct, fxam, fxtract, esp - 18 digits
559  cycles, ConvertFloat8DY, direct, examine, fxtract, esp - 18 digits
615  cycles, ConvertFloat8DR, direct, examine, fxtract, ebp - 18 digits
680  cycles, ConvertFloat8DS, direct, fxam, fyl2x, ebp - 18 digits
693  cycles, ConvertFloat8CTD, BCD-CT, fxam, fxtract, esp - 18 digits
701  cycles, ConvertFloat8BFD, BCD, fxam, fxtract, esp - 18 digits
702  cycles, ConvertFloat8BY, BCD, examine, fxtract, esp - 18 digits
703  cycles, ConvertFloat8BF, BCD, fxam, fxtract, esp - 18 digits
709  cycles, ConvertFloat8BYD, BCD, examine, fxtract, esp - 18 digits
712  cycles, ConvertFloat8BXD, BCD, fxam, fxtract, ebp - 18 digits
715  cycles, ConvertFloat8DF, direct, examine, fyl2x, ebp - 18 digits
719  cycles, ConvertFloat8BX, BCD, fxam, fxtract, ebp - 18 digits
726  cycles, ConvertFloat8CT, BCD-CT, fxam, fxtract, esp - 18 digits
1151  cycles, ConvertFloat8ZX, BCD - old - 18 digits
1163  cycles, ConvertFloat8Z, BCD -old - 18 digits
2580  cycles, ConvertFloat8DWD, direct,Save FPU, fxam, fxtract, esp -18 digits
2916  cycles, ConvertFloat8BWD, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
2980  cycles, ConvertFloat8DW, direct,Save FPU, fxam, fxtract, esp -18 digits
3020  cycles, ConvertFloat8, BCD, Save FPU -old - 18 digits
3039  cycles, ConvertFloat8BW, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
 ********** END **********

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Converting real8 to string
« Reply #5 on: May 01, 2013, 03:00:52 AM »
 :biggrin:
Here are some numbers. Any question, please... ?

note 1: if Convert... ends with 3 letters (e.g. ...DFD), the last is D=decimal places we want
note 2: the number used : _Real8_2  dq -12345678.56781234
note 3: i am using the macros from MichaelW for timing

Quote
3.14159265358979312 <--  by ConvertFloat8D?
 3.14159265358979312
 3.14159265358979312
 3.14159265358979312
 3.14159265358979312
 3.14159265358979312
3.141593                            <--------- from fptoa
 *** STOP - Converting PI ***
1168 cycles, ConvertFloat8ZX, _Real8_2

3015 cycles, ConvertFloat8, _Real8_2

1161 cycles, ConvertFloat8Z, _Real8_2

682 cycles, ConvertFloat8DF, _Real8_2

607 cycles, ConvertFloat8DR, _Real8_2

716 cycles, ConvertFloat8BX, _Real8_2

706 cycles, ConvertFloat8BF, _Real8_2

702 cycles, ConvertFloat8BY, _Real8_2

561 cycles, ConvertFloat8DY, _Real8_2

560 cycles, ConvertFloat8DX, _Real8_2

783 cycles, ConvertFloat8CT, _Real8_2

683 cycles, ConvertFloat8DS, _Real8_2

3059 cycles, ConvertFloat8BW, _Real8_2

2983 cycles, ConvertFloat8DW, _Real8_2

454 cycles, ConvertFloat8DFD, _Real8_2

333 cycles, ConvertFloat8DRD, _Real8_2

707 cycles, ConvertFloat8BXD, _Real8_2

702 cycles, ConvertFloat8BFD, _Real8_2

704 cycles, ConvertFloat8BYD, _Real8_2

329 cycles, ConvertFloat8DYD, _Real8_2

325 cycles, ConvertFloat8DXD, _Real8_2

706 cycles, ConvertFloat8CTD, _Real8_2

498 cycles, ConvertFloat8DSD, _Real8_2

2924 cycles, ConvertFloat8BWD, _Real8_2

2591 cycles, ConvertFloat8DWD, _Real8_2

2216 cycles, TimFloatToStr, _Real8_2

 *** Press any key to get the time table ***

 ***** Time table *****

325  cycles, ConvertFloat8DXD, direct, fxam, fxtract, esp - 18 digits
329  cycles, ConvertFloat8DYD, direct, examine, fxtract, esp - 18 digits
333  cycles, ConvertFloat8DRD, direct, examine, fxtract, ebp - 18 digits
454  cycles, ConvertFloat8DFD, direct, examine, fyl2x, ebp - 18 digits
498  cycles, ConvertFloat8DSD, direct, fxam, fyl2x, ebp - 18 digits
560  cycles, ConvertFloat8DX, direct, fxam, fxtract, esp - 18 digits
561  cycles, ConvertFloat8DY, direct, examine, fxtract, esp - 18 digits
607  cycles, ConvertFloat8DR, direct, examine, fxtract, ebp - 18 digits
682  cycles, ConvertFloat8DF, direct, examine, fyl2x, ebp - 18 digits
683  cycles, ConvertFloat8DS, direct, fxam, fyl2x, ebp - 18 digits
702  cycles, ConvertFloat8BFD, BCD, fxam, fxtract, esp - 18 digits
702  cycles, ConvertFloat8BY, BCD, examine, fxtract, esp - 18 digits
704  cycles, ConvertFloat8BYD, BCD, examine, fxtract, esp - 18 digits
706  cycles, ConvertFloat8CTD, BCD-CT, fxam, fxtract, esp - 18 digits
706  cycles, ConvertFloat8BF, BCD, fxam, fxtract, esp - 18 digits
707  cycles, ConvertFloat8BXD, BCD, fxam, fxtract, ebp - 18 digits
716  cycles, ConvertFloat8BX, BCD, fxam, fxtract, ebp - 18 digits
783  cycles, ConvertFloat8CT, BCD-CT, fxam, fxtract, esp - 18 digits
1161  cycles, ConvertFloat8Z, BCD -my old one- 18 digits
1168  cycles, ConvertFloat8ZX, BCD - old - 18 digits
2216  cycles, TimFloatToStr or fptoa, -18 digits
2591  cycles, ConvertFloat8DWD, direct,Save FPU, fxam, fxtract, esp -18 digits
2924  cycles, ConvertFloat8BWD, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
2983  cycles, ConvertFloat8DW, direct,Save FPU, fxam, fxtract, esp -18 digits
3015  cycles, ConvertFloat8, BCD, Save FPU -old - 18 digits
3059  cycles, ConvertFloat8BW, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
 ********** END **********

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Converting real8 to string
« Reply #6 on: May 01, 2013, 03:08:30 AM »

I try a question,...
Or perhaps better than the fcvt for the crt ?
               i never used it if anyone tell me how do to
               or what to do i tested it. Please, tell me what i should do.

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Converting real8 to string
« Reply #7 on: May 01, 2013, 03:14:58 AM »
Well done. :t

Gunther
                Hi Gunther  :t

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Converting real8 to string
« Reply #8 on: May 01, 2013, 03:16:06 AM »
looking good, Rui   :t
               Hey, Dave, are you well ? I hope so much  :t

ToutEnMasm

  • Member
  • *****
  • Posts: 1190
    • EditMasm
Re: Converting real8 to string
« Reply #9 on: May 01, 2013, 03:53:50 AM »
Quote
ConvertFloat8DS                              3.141592653589793
sprintf_s                                           3.1415926535897931

Your function write outside the buffer,at the beginning (3,4 bytes)
and show 1 digit less than sprintf_s
Fa is a musical note to play with CL

jj2007

  • Member
  • *****
  • Posts: 8773
  • Assembler is fun ;-)
    • MasmBasic
Re: Converting real8 to string
« Reply #10 on: May 01, 2013, 04:56:57 AM »
Which means that Rui's version is better because the last digit, 1, is wrong.

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Converting real8 to string
« Reply #11 on: May 01, 2013, 05:06:42 AM »
Quote
ConvertFloat8DS                              3.141592653589793
sprintf_s                                           3.1415926535897931

Your function write outside the buffer,at the beginning (3,4 bytes)
and show 1 digit less than sprintf_s
               Why it writes outside the buffer ?
The buffer MUST BE LIKE THIS:
                                                                 dd ?        <- to get the length
                                                   _buffer   db 30 dup (?)

                       dont forget this dd before the address
About PI
                if you specify 16 decimal places => 17 digits

               it gives 3.1415926535897931
               ;*** STOP - ConvertFloat8DS  PI- 17 digits ***

If you use the converter from real10 i posted we get
                            ;3.14159265358979324
                            ;*** STOP - Converting PI ***

So the last 1 is error as Jochen said. This is why i use only 15 decimal places, 16 digits in real8.

note: after conversion, if we do, for instance, mov ecx, [_buffer-4] we get
          how long is the string, we get the length. Note that the buffer is also
          null terminated.
« Last Edit: May 01, 2013, 07:13:55 AM by RuiLoureiro »

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Converting real8 to string
« Reply #12 on: May 01, 2013, 05:21:52 AM »
Which means that Rui's version is better because the last digit, 1, is wrong.
                  Very well, Jochen  :t  :eusa_clap:

dedndave

  • Member
  • *****
  • Posts: 8808
  • Still using Abacus 2.0
    • DednDave
Re: Converting real8 to string
« Reply #13 on: May 01, 2013, 08:24:31 AM »
Quote
Hey, Dave, are you well ?

feeling better today, Rui - thank you for asking
i will be back to normal in a couple days or so   :P

Code: [Select]
ConvertFloat8DS                              3.141592653589793
sprintf_s                                    3.1415926535897931

this is one of the finer points where Jochen and i disagree
the last digit does not to be correct to be considered "usable"

if we look at a few more digits of Pi...
Code: [Select]
3.1415926535897932385you can see that 3.1415926535897931 is closer to the actual value of Pi than 3.141592653589793
so, even though the digit is incorrect, it is usable
in fact, any value from 3.1415926535897931 to 3.1415926535897934 is closer than 3.141592653589793

to understand the logic in this.....
it is a 1 vs 2 digit because we happen to be converting to base 10
if we were converting to some other base, it may actually be a correct digit
should we disregard extra information, simply because we chose base 10 ?
the fact that we are converting to base 10 should be somewhat inconsequential
of course, we have to end the string someplace, so the base does make a difference

in truth, we shouldn't look at real Pi, we should look at the numbers that may be represented with extra precision   :P
Code: [Select]
400921FB_54442D17: +3.1415926535897926719
400921FB_54442D18: +3.141592653589793116
400921FB_54442D19: +3.1415926535897935601
...31 is as close as we can get with real8

ToutEnMasm

  • Member
  • *****
  • Posts: 1190
    • EditMasm
Re: Converting real8 to string
« Reply #14 on: May 01, 2013, 02:59:09 PM »
Quote
The buffer MUST BE LIKE THIS:
                                                                 dd ?        <- to get the length
                                                   _buffer   db 30 dup (?)

I suggest you modify the prototype of your function using a structure.
for example:
    STRFLOAT STRUCT
           long DWORD ?
           strf  BYTE MAX_PATH dup (?)
    STRFLOAT ENDS   

It's the first time i see something working like that:
12 00 00 00 20 33 2e 31-34 31 35 39 32 36 35 33  .... 3.141592653
0040430e  35 38 39 37 39 33 00 00-00 00 00 00 00 00 00 00  589793..........

The address passed to the function must be STRFLOAT.strf and not STRFLOAT
Not very usable







Fa is a musical note to play with CL