Thread: Float to ASCII

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Float to ASCII

    Can someone please share the logic for converting a float into a string of ASCII characters, without using standard functions....

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Just like what function itoa() does to an integer, is their any float counterpart.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Have you looked in your C library documentation?

    Any of these conversions is relatively easy to accomplish using sprintf()... look it up, you'll see.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    itoa() is not C standard

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    23
    Quote Originally Posted by juice View Post
    Can someone please share the logic for converting a float into a string of ASCII characters, without using standard functions....
    If you want to convert an float to a string you could use sprintf.
    Code:
    float myFloat = 3.23;
    char myString[30];
    sprintf(myString, "%f", myFloat);
    See more at sprintf - C++ Reference

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    itoa - Wikipedia, the free encyclopedia

    An itoa function (and a similar function, ftoa, that converted a float to a string) was listed in the first-edition Unix manual.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    juice, converting float to string is very difficult in the general case.
    I looked into doing it for my custom template-sized floating point class and ended up still having not attempted that part myself. It usually involves using a bignum data type as a temporary.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by stahta01
    An itoa function (and a similar function, ftoa, that converted a float to a string) was listed in the first-edition Unix manual.
    That does not make it part of the C standard library, which is what "standard" means in this forum with respect to a function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by laserlight View Post
    That does not make it part of the C standard library, which is what "standard" means in this forum with respect to a function.
    The original poster did NOT want a standard function. So, I posted a function name likely to get results if he/she looks hard enough.

    Tim S.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    The original poster did NOT want a standard function. So, I posted a function name likely to get results if he/she looks hard enough.
    The original poster wanted the logic, not hints about non-standard functions.

    To answer the original question.

    There are generally three fields of bits within a floating point variable. They are the sign (determines if the value is positive or negative), a mantissa, and and exponent. If we assume the base is 10, and the floating point type is symmetric (i.e. all representable negative values can be converted to a representable positive value by multiplying by -1) then

    1) The floating point type has three fields: a sign (typically represented using a bit where on means positive and off means negative, or vice versa), a mantissa (which is in the range 0 to 1, not including the value 1), and an exponent (which is a signed integer field). The "value" of the floating point variable is determined by sign * mantissa * 10 ^ exponent (where I use * to represent multiplication and ^ to represent "to the power of").

    2) The basic logic of printing out a floating point variable is to interpret those three fields separately. For example, using printf()'s %g format, you might see output in the form -0.20E+30 The negative is the sign, 0.20 is the mantissa, and +30 is the exponent.

    Practically, the the representation (layout of the fields, number of bits used to represent mantissa and exponent) are implementation defined (each floating point representation lays things out differently).

    Keep in mind I've made vastly simplifying assumptions in the above, such as base 10 and symmetric values. Those assumptions are common in practice, but the C standard does not actually require them. Some floating point representations, such as those defined by IEEE, also support NaN (non-a-number), infinities, and other types of values. Those usages require the floating point type to reserve some bits for those usage (which reduces the number of bits used for mantissa and/or exponent). The C standard does not require such things, but does not prevent them either.

    In short, printing out a floating point value requires interpreting the various bit-fields in it in order to print out the "value". The actual bit-fields that need to be interpreted, and their layout within a floating point variable, vary between representations.

    In practice, the reason people will tell you to use standard methods is that floating point representations vary between systems, so the precise workings to print out a floating point value vary between systems. If you to do it by hand, your code will probably break when your port it (or, possibly, even if you change compilation settings related to floating point).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by juice View Post
    Just like what function itoa() does to an integer, is their any float counterpart.
    Sorry, that I answered this question; I will ignore this thread in the future.

    Tim S.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stahta01 View Post
    Sorry, that I answered this question; I will ignore this thread in the future.
    Tim S.
    Oh boy... I wish I had a dollar for every time I've felt that way around here....

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Thanks grumpy, I just needed the logic to implement the fuction ftoa(), which is the float counterpart of itoa(), as stahta informed above in the 6th post.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The easiest oprtion is to get a bigint library and assuming that the float holds a number rather than inf/nan, convert the float to a bigint and then convert the bigint into decimal digits. Then insert the decimal point in the correct place and prepend a minus sign is necessary.
    That's the basics, although proper implementations have all sorts of optimisations, plus tweaks that guarantee that converting the resulting string back into a float gives you the exact same binary value, whilst also guaranteeing a minimal number of digits to do so, and stuff like that.
    Unlike converting an int to a string which is totally basic, float to string is extremely complex.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    It's relatively easy if you think about it. For example:

    123.456 = 123 + 0.456 => cout << 123 << '.';
    0.456*10 = 4 + 0.56 => cout << 4;
    0.56*10 = 5 + 0.6 => cout << 5;
    0.6*10 = 6 + 0.0 => cout << 6;
    0.0 => end

    Printed: 123.456

    It's really that simple!
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (float) or (float*) wrt sizeOf()
    By jakemott in forum C Programming
    Replies: 2
    Last Post: 07-19-2009, 08:13 PM
  2. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  3. ASCII to float
    By trucutu in forum C Programming
    Replies: 5
    Last Post: 11-14-2005, 06:25 PM
  4. Replies: 8
    Last Post: 07-08-2005, 09:12 AM
  5. Unresolved external 'convert(float, float)'
    By Ipsec Espah in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2003, 10:08 AM