DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   String to Double Conversion without using strtod() or atof() (http://www.daniweb.com/forums/thread159437.html)

JCasso Nov 25th, 2008 11:59 am
String to Double Conversion without using strtod() or atof()
 
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.

Murtan Nov 25th, 2008 12:20 pm
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.

Ancient Dragon Nov 25th, 2008 12:34 pm
Re: String to Double Conversion without using strtod() or atof()
 
use a pointer and convert each digit to binary. Here's how:
char str[] = "123.456";
char* ptr = str;
float n = (*ptr - '0'); // before the decimal point

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.

JCasso Nov 25th, 2008 3:48 pm
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.

ArkM Nov 25th, 2008 7:03 pm
Re: String to Double Conversion without using strtod() or atof()
 
Quote:

Originally Posted by JCasso (Post 744426)
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...

JCasso Nov 25th, 2008 7:08 pm
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.

Quote:

Originally Posted by ArkM (Post 744693)
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...


Salem Nov 26th, 2008 2:03 am
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.

ArkM Nov 26th, 2008 4:56 am
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.

JCasso Nov 26th, 2008 6:39 am
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);

ArkM Nov 26th, 2008 8:28 am
Re: String to Double Conversion without using strtod() or atof()
 
Yes, this POS behaves suspiciously ;(
Try not-null endptr argument...
FPU?..


All times are GMT -4. The time now is 10:00 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC