string to floating point number conversion

developing a function to convert a string to a floating point number

Page 1 of 1

6 Replies - 3364 Views - Last Post: 25 March 2009 - 12:26 PM Rate Topic: -----

#1 ihatestarch  Icon User is offline

  • New D.I.C Head

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

string to floating point number conversion

Post icon  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  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: string to floating point number conversion

Posted 24 March 2009 - 10:00 PM

View Postihatestarch, 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

Was This Post Helpful? 1

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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).
Was This Post Helpful? 1

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: string to floating point number conversion

Posted 25 March 2009 - 09:40 AM

View PostNickDMax, 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.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#7 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: string to floating point number conversion

Posted 25 March 2009 - 12:26 PM

View PostNickDMax, 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1