Results 1 to 7 of 7

Thread: Double to string

  1. #1

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  2. #2
    jim mcnamara
    Guest
    Ked gave you ecvt()

    It actually does something like this:
    Code:
    char t[20];
    double z=10;
    sprintf(t,"%f",z);

  3. #3

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Jim, whats so cool about using sprintf, it has to interpret the "%f" at runtime right?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    jim mcnamara
    Guest
    No. the compiler takes all of the stuff that is pre-defined like %f and provides a vector directly to the conversion routine. If the string is _T or WCHAR then it gets even messier and uses another routine.

    If you play around like this: "%5.2f", then the code doesn't go directly to conversion at runtime. You can look at the asm c++ generates and see this. Change the compile options to produce an asm list.

    Any numeric -> string conversion involves a fair amount of overhead. Formatting in general is very expensive. You can look at this as just formatting with type conversion.

  5. #5

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    ; 138  : 	char t[20];
    ; 139  : 	double z=10;
    ; 140  : 	sprintf(t,"%f",z);  
    
    	lea	eax, DWORD PTR _t$[esp+20]
    	push	1076101120				; 40240000H
    	push	0
    	push	OFFSET FLAT:??_C@_02JBAA@?$CFf?$AA@	; `string'
    	push	eax
    	call	_sprintf
    	add	esp, 16					; 00000010H
    Not that i understand why the parameters but _sprintf gets called and only other reference in the listing is
    EXTRN _sprintf:NEAR
    same thing here:
    Code:
    ; 138  : 	char t[20];
    ; 139  : 	double z=10;
    ; 140  : 	sprintf(t,"%5.2f",z);  
    
    	lea	eax, DWORD PTR _t$[esp+20]
    	push	1076101120				; 40240000H
    	push	0
    	push	OFFSET FLAT:??_C@_05MPII@?$CF5?42f?$AA@	; `string'
    	push	eax
    	call	_sprintf
    	add	esp, 16					; 00000010H
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    jim mcnamara
    Guest
    That's interesting. The last time I tried this , there were big differences in the entry point call. I wonder why. Sorry.

    I don't have an answer, unless the compiler or the _sprintf entry point have changed since I last played with it.

    We were testing performance and found the "%5.2f" was slower than "%f" for several hundred thousand iterations. So, we looked at the asm, and there was a load of difference. We were converting hundreds of millions of IBM BCD's to doubles, doing some calculations, then to strings and inserting them into a DB. The strings had to be a fixed format.

  7. #7

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well, i suppose you won't need that parameter
    Code:
    ; 138  : 	char t[20];
    ; 139  : 	sprintf(t,"%5.2f");  
    
    	lea	eax, DWORD PTR _t$[esp+20]
    	push	OFFSET FLAT:??_C@_05MPII@?$CF5?42f?$AA@	; `string'
    	push	eax
    	call	_sprintf
    	add	esp, 8
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width