943,545 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4125
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 25th, 2008
0

String to Double Conversion without using strtod() or atof()

Expand Post »
Hi,

I am programming a pos terminal by using C Programming language. I cannot use strtod() or atof (since it just calls strtod in it).

Any advices on converting string to double without using these functions?

Thanks in advance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JCasso is offline Offline
7 posts
since Nov 2008
Nov 25th, 2008
0

Re: String to Double Conversion without using strtod() or atof()

This sounds like an assignment, but you will probably have to re-create what strtod() does.

You will end up validating the string (or characters) and building the number by recognizing the digit characters.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Nov 25th, 2008
0

Re: String to Double Conversion without using strtod() or atof()

use a pointer and convert each digit to binary. Here's how:
  1. char str[] = "123.456";
  2. char* ptr = str;
  3. float n = (*ptr - '0'); // before the decimal point
  4.  
  5. float n = (*ptr - '0') / 10; // after the decimal point

Now, what I posted above is not a complete program -- I purposely left out important code that will make it work. You also have to have a couple loops and change 10 to a variable that is incremented by 10 on each loop iteration.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Nov 25th, 2008
0

Re: String to Double Conversion without using strtod() or atof()

Ancient Dragon,

It seems that it will end up with some function like you proposed. I will wait a few hours more for some possible ideas and mark this as solved.

Thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JCasso is offline Offline
7 posts
since Nov 2008
Nov 25th, 2008
0

Re: String to Double Conversion without using strtod() or atof()

Click to Expand / Collapse  Quote originally posted by JCasso ...
Hi,

I am programming a pos terminal by using C Programming language. I cannot use strtod() or atof (since it just calls strtod in it).

Any advices on converting string to double without using these functions?
Well, use sscanf.
I think it's an incorrect question. Why pos terminal control program can't use strtod? Is it possible to use sscanf? Or you MUST write C-string to double conversion code from the scratch? If so, it's the other story...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 25th, 2008
0

Re: String to Double Conversion without using strtod() or atof()

Using strtod causes a segment error, odd but true. About sscanf... Shame on me You are right. sscanf can do it.

Click to Expand / Collapse  Quote originally posted by ArkM ...
Well, use sscanf.
I think it's an incorrect question. Why pos terminal control program can't use strtod? Is it possible to use sscanf? Or you MUST write C-string to double conversion code from the scratch? If so, it's the other story...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JCasso is offline Offline
7 posts
since Nov 2008
Nov 26th, 2008
0

Re: String to Double Conversion without using strtod() or atof()

> Using strtod causes a segment error,
If that's your reason for NOT using it, then you've got bugs in your code. Simply using another function does not make those bugs go away.

All that will happen is that sooner or later, you'll stumble across some other problem. Then, as now, you'll imagine the problem is somewhere else, and ask how to do x without using y.

A simple 5-line program which calls strtod() on your target would prove whether the library for your target was working or not. If the simple test works, and using strtod() in your code doesn't, then you really need to look at your code.

Valid reasons for not using strtod() on a PoS target would be
- too many problems with rounding for numeric values (but that's just floats all over)
- code footprint of the strtod() function is too large.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 26th, 2008
0

Re: String to Double Conversion without using strtod() or atof()

I agree with Salem. I have wrote POS terminal control program (some years ago). As usually, no problems with memory and floating point arithmetics on POS processors. Moreover, if strtod is too expensive then sscanf is too expensive, printf is too expensive and so on. Impossible. It's not so time/space consuming code.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 26th, 2008
0

Re: String to Double Conversion without using strtod() or atof()

Salem,

I already tried creating a small app which calls strtod. And it failed. Besides, my app works on simulator, I guess this problem is not about my code.

ArkM,

Possibly you've programmed a different brand by using different SDK.

I just defined a double variable, a char array. An called strtod. Works on simulator but not on the device.

Here is the code:

double d;
char s[] = "0.5";
d = strtod(s, NULL);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JCasso is offline Offline
7 posts
since Nov 2008
Nov 26th, 2008
0

Re: String to Double Conversion without using strtod() or atof()

Yes, this POS behaves suspiciously ;(
Try not-null endptr argument...
FPU?..
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Pointer arrays and structures
Next Thread in C Forum Timeline: need help with validation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC