# string to floating point number conversion

Page 1 of 1

## 6 Replies - 4139 Views - Last Post: 25 March 2009 - 12:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=95182&amp;s=c92069f7483ee02c380e8b09821d2411&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ihatestarch

Reputation: 0
• Posts: 6
• Joined: 06-February 09

# string to floating point number conversion

Posted 24 March 2009 - 09:56 PM

I'm not suppose to use any functions already made to convert a string number to a floating point number. I'm suppose to develop one. I'm having a hard time.

So far I have switch statement with the different cases for string numbers '1'-'9' that saves the actual number into a double. I'm not sure where to go from there or if I'm doing the right thing. Our hint for the assignment was using the string's length and at methods.

I figured the length method is used to set the length of the string and the at method I used in my switch statement.

What should I do?? I couldn't find an example of an already made function. I'm super stuck. Any hints??

Is This A Good Question/Topic? 0

## Replies To: string to floating point number conversion

### #2 ccubed

• It's That Guy

Reputation: 164
• Posts: 1,416
• Joined: 13-June 08

## Re: string to floating point number conversion

Posted 24 March 2009 - 10:00 PM

ihatestarch, on 24 Mar, 2009 - 08:56 PM, said:

I'm not suppose to use any functions already made to convert a string number to a floating point number. I'm suppose to develop one. I'm having a hard time.

So far I have switch statement with the different cases for string numbers '1'-'9' that saves the actual number into a double. I'm not sure where to go from there or if I'm doing the right thing. Our hint for the assignment was using the string's length and at methods.

I figured the length method is used to set the length of the string and the at method I used in my switch statement.

What should I do?? I couldn't find an example of an already made function. I'm super stuck. Any hints??

Remember that decimal means base 10 so that 100 is

1x10^2 + 0x10^1 + 0x10^0

Also, 100.02 is equivalent to

1x10^2 + 0x10^1 + 0x10^0 + 0x10^-1 + 2x10^-2

This post has been edited by ccubed: 24 March 2009 - 10:04 PM

### #3 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: string to floating point number conversion

Posted 25 March 2009 - 05:00 AM

Generally I use atof to do my conversion.

in the snippets section I have a snippet that parses complex numbers. This in and of itself is no use to you, but part of that is parsing out floating point numbers as well. The function getRealNumber() does not actually do the conversion but it does parse out the format which could be modified to actually calculate the number (rather than just count up the characters that it occupies).

I also have a snippet that parses an integer -- this would be similar to how you might create a function to parse a floating point number.

The other thing you can do is look at the source for atof. This is only one implementation (there are others).

Basically to parse a number I find that using a Finite State Machine works well. They are pretty darn fast and although they look really complicated when you code them, they are actually really easy once you understand them. They can also be implemented using tables which can increase the speed (but makes them virtually un-human readable).

### #4 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: string to floating point number conversion

Posted 25 March 2009 - 05:08 AM

Here is ok example: strtod

The technique is ok, but they didn't do any error checking.

### #5 ccubed

• It's That Guy

Reputation: 164
• Posts: 1,416
• Joined: 13-June 08

## Re: string to floating point number conversion

Posted 25 March 2009 - 09:40 AM

NickDMax, on 25 Mar, 2009 - 04:08 AM, said:

Here is ok example: strtod

The technique is ok, but they didn't do any error checking.

I don't claim to be an expert, but this seems like an overly complicated solution to an overly simple problem. For example, take a string of length 5, that contains the number 20,450. Now take this algorithm:
```int conversion = 0;
int power = len(string)-1;
For( i(0) i < len(string) i++)
conversion += atoi(string[i]) * (10^power);
power--

```

Note: that's pseudo-code because I know ^ doesn't exist. I think it may be **

And there we go. Atoi can even be coded fairly simple as well if you wanted to make your own. Not to mention error checking is easier. if the len is bigger than 5, obviously it's too big. If the first number is bigger than 3, once again, too big.

note: I'm assuming the portable int value of 32,767.

### #6 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: string to floating point number conversion

Posted 25 March 2009 - 10:00 AM

You can also do it this way. Basically in the end they work out to the same basic thing since atoi is also a FSM. However you are right. (note that the pow() function is what you were thinking of).

The strtod actually may work faster then using 2 atoi functions...

### #7 ccubed

• It's That Guy

Reputation: 164
• Posts: 1,416
• Joined: 13-June 08

## Re: string to floating point number conversion

Posted 25 March 2009 - 12:26 PM

NickDMax, on 25 Mar, 2009 - 09:00 AM, said:

You can also do it this way. Basically in the end they work out to the same basic thing since atoi is also a FSM. However you are right. (note that the pow() function is what you were thinking of).

The strtod actually may work faster then using 2 atoi functions...

I was under the impression that pow() was a double function. Is there an integer version?