Click to See Complete Forum and Search --> : Double to string
kedaman
Dec 4th, 2001, 10:05 PM
http://www.cplusplus.com/ref/cstdlib/ecvt.html
jim mcnamara
Dec 5th, 2001, 12:32 PM
Ked gave you ecvt()
It actually does something like this:
char t[20];
double z=10;
sprintf(t,"%f",z);
kedaman
Dec 5th, 2001, 12:46 PM
Jim, whats so cool about using sprintf, it has to interpret the "%f" at runtime right?
jim mcnamara
Dec 5th, 2001, 01:04 PM
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.
kedaman
Dec 5th, 2001, 01:28 PM
; 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:
; 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
jim mcnamara
Dec 5th, 2001, 02:00 PM
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.
kedaman
Dec 5th, 2001, 02:05 PM
Well, i suppose you won't need that parameter :p
; 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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.