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

Reply

Join Date: Nov 2008
Posts: 7
Reputation: JCasso is an unknown quantity at this point 
Solved Threads: 0
JCasso JCasso is offline Offline
Newbie Poster

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

 
0
  #1
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

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

 
0
  #2
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,363
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

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

 
0
  #3
Nov 25th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: JCasso is an unknown quantity at this point 
Solved Threads: 0
JCasso JCasso is offline Offline
Newbie Poster

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

 
0
  #4
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #5
Nov 25th, 2008
Originally Posted by JCasso View 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?
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...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: JCasso is an unknown quantity at this point 
Solved Threads: 0
JCasso JCasso is offline Offline
Newbie Poster

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

 
0
  #6
Nov 25th, 2008
Using strtod causes a segment error, odd but true. About sscanf... Shame on me You are right. sscanf can do it.

Originally Posted by ArkM View Post
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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #7
Nov 26th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #8
Nov 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: JCasso is an unknown quantity at this point 
Solved Threads: 0
JCasso JCasso is offline Offline
Newbie Poster

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

 
0
  #9
Nov 26th, 2008
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);
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #10
Nov 26th, 2008
Yes, this POS behaves suspiciously ;(
Try not-null endptr argument...
FPU?..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC