473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Conversion Part 2: A better way?

Hi all,

Sorry for asking a second question today.

I have the task of reading a text file with a string of length x on
each line. My task is to convert x to a double between 0 and 1. For
example:

File Output
123 0.123
6786 0.6786

I have the following code, which seems to work, but it strikes me
there may be a better way that avoids so many concatanations.

double convert(string s)
{
double x;
string str = "." + s;
x = atof(str.c_str());
return x;
}
Thanks for your time again.

Jim

Sep 27 '07 #1
4 1396
Evyn wrote:
Sorry for asking a second question today.
.... and try not to post twice in five minutes...
I have the task of reading a text file with a string of length x on
each line. My task is to convert x to a double between 0 and 1. For
example:

File Output
123 0.123
6786 0.6786

I have the following code, which seems to work, but it strikes me
there may be a better way that avoids so many concatanations.
"So many"? Not sure what you mean. How many concatenations do you
think you have here? I counted one operator+.

If your buffer is long enough (and using 'std::string' has the
advantage of not pre-allocating the buffer), you could pre-stuff it
with the '.' and read into it starting from the second character,
thus appending at the time of reading, but that requires knowing
the buffer size ahead of time. 'std::string' has no such limitation.
double convert(string s)
If you don't change 's' itself (you could), then it's better to pass
it by ref to const:

double convert(string const& s)
{
double x;
string str = "." + s;
x = atof(str.c_str());
return x;
Rewrite it in a single line:

return atof((string(".") + s).c_str());
}
Thanks for your time again.
You can also rewrite it like so:

double convert(string s) // not ref to const
{
s.insert(s.begin(), '.');
return atof(s.c_str());
}

Whether it's going to be faster or not is unknown, can only be
determined by profiling.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #2
"So many"? Not sure what you mean. How many concatenations do you
think you have here? I counted one operator+.
Thanks for the help.

By "so many", I refer to the fact that this method will be called for
each line in the file (100,000+), so there would be many concats.

Jim

Sep 27 '07 #3
Evyn wrote:
>"So many"? Not sure what you mean. How many concatenations do you
think you have here? I counted one operator+.

Thanks for the help.

By "so many", I refer to the fact that this method will be called for
each line in the file (100,000+), so there would be many concats.
An alternative is to have a buffer of sufficient length, set its first
character to '.', and read *into it* starting from the second element.
Then pass that buffer to 'atof'. Now, do you expect any of the input
numbers to be negative?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '07 #4
On Sep 27, 3:59 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Now, do you expect any of the input numbers to be negative?
No. But of course, errors happen.

Jim.

Sep 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Martin Trautmann | last post by:
Hi all, is there any kind of 'hiconv' or other (unix-like) conversion tool that would convert UTF-8 to HTML (ISO-Latin-1 and Unicode)? The database output is UTF-8 or UTF-16 only - Thus almost...
26
by: David W. Fenton | last post by:
A client is panicking about their large Access application, which has been running smoothly with 100s of thousands of records for quite some time. They have a big project in the next year that will...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
7
by: CBFalconer | last post by:
Consider: #include <stdlib.h> /* An elementary optimizer is expected to remove most code */ /* Return the absolute value */ unsigned long ulabs(long j) { if (0 == LONG_MIN + LONG_MAX) { if...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
12
by: Daniel Walzenbach | last post by:
Hi, I want to display a Label in a DataGrid according to some condition. I therefore check whether the condition is true in the ItemDateBound EventHandler of the DataGrid. Unfortunately the...
47
by: rawCoder | last post by:
Hi, Just wanted to know if there is any speed difference between VB conversion Keywords like CInt, Clng, CStr, CDbl, CBool etc. ..NETs Convert.To<...> methods. And which is better to be...
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.