Re: Converting double to widestring

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Alf P. Steinbach wrote:
* Ed:
We have an application that converts a double to widestring (std::wstring). This happens thousands of time. We also need to provide how many decimal places to be used. The decimal point needs to be a period regardless of the locale of the machine. The function we call has signature like ConverDouble(std::wstring & str, double d, long digits)

What is the fastest way to do it?

First, be aware that you're likely heading for a case of evil premature optimization. Thousands of times is nothing, absolutely nothing, not even an eyeblink for the user. But given that you have measured and found that this is indeed a bottleneck that really really needs some optimization:

be aware that dynamic allocation for the std::wstring is likely to use orders of magnitude more time than the double->string conversion. So I'd suggest using a static buffer. For that, be aware of thread safety issues. An alternative, if this function is called in a tight loop in one place (or two, or three), is to only let the function resize the string and optimize at the caller site, where the same string variable should then be re-used repeatedly.

And given that, I'd prefer to suggest using the internal function that swprintf uses, namely _cfltcvt, which is just a macro that in turn references the first function pointer in array _cfltcvt_tab. However, when checking that out (that's how I found the names) the linker protested that it couldn't find this symbol. Not sure what needs to be linked, or how the heck it's found for printf itself.

If you find out how to use the _cfltcvt function, please report the details (of linking) here! :-)

Given that the _cfltcvt function is practically unavailable, I suggest plain wsprintf, which doesn't really add that much extra processing, only a little. A suitable format might be "%.*g", with precision as int argument. Note that your function signature has long precision (assuming "digits" means precision), and that that is most probably extreme overkill and Bad Design, but that to get rid of the dynamic allocation for wstring the signature may need to be adjusted anyway -- and I'd also suggest fixing the name, which says nothing whatsoever.

Then, again, measure.


Cheers, & hth.,

- Alf



Thanks for the reply. I removed some detail from my post for simplification. The actual function name is AppendDoubleToWideString. The function append the double to the string. The string is allocated in the caller and has more than enough space. I am currently using wsprintf but I will look at your suggestion too.

How about having a period for decimal notation even if the software is used in a, for example, Spanish machine?

Thanks again.
.



Relevant Pages

  • Re: Converting double to widestring
    ... Given that the _cfltcvt function is practically unavailable, I suggest plain wsprintf, which doesn't really add that much extra processing, only a little. ... Note that your function signature has long precision, and that that is most probably extreme overkill and Bad Design, but that to get rid of the dynamic allocation for wstring the signature may need to be adjusted anyway -- and I'd also suggest fixing the name, which says nothing whatsoever. ...
    (microsoft.public.vc.language)
  • Re: Why (0.09+0.01-0.1) is not equal to 0.09+0.01-0.1 ?
    ... > gives full accuracy by default, but also includes rounding algorithms ... > partitioned mantissa by without risk of overflow ... > Function D2BAs String ... the work involved in avoiding lost precision due to type ...
    (microsoft.public.excel)
  • Re: Precision vs. Accuracy and Significant Figures
    ... String category; ... add(UnitOfMeasure, UnitOfMeasure); ... units and just spit out an answer to the proper precision if all the ... gives more information than dealing with significant digits. ...
    (comp.lang.java.advocacy)
  • Re: Why (0.09+0.01-0.1) is not equal to 0.09+0.01-0.1 ?
    ... However less precision would suffice, since 17 digits would uniquely identify an IEEE double precision binary representation. ... Function D2BAs String ... You can use the VBA Decimal data type to carry 28 figures, but you have to be careful to avoid truncation in type conversions and to avoid overflow or underflow since the Decimal data type has fixed precision with no scientific notation. ...
    (microsoft.public.excel)
  • Re: Precision problem
    ... > so that i am converting it to string using gcvtand sprintfetc. ... > After this some precision loss is happening. ... floating point value that's turning out different from your expectations, ...
    (microsoft.public.vc.language)