Can someone please share the logic for converting a float into a string of ASCII characters, without using standard functions....
This is a discussion on Float to ASCII within the C Programming forums, part of the General Programming Boards category; Can someone please share the logic for converting a float into a string of ASCII characters, without using standard functions.......
Can someone please share the logic for converting a float into a string of ASCII characters, without using standard functions....
Just like what function itoa() does to an integer, is their any float counterpart.
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.
itoa() is not C standard
If you want to convert an float to a string you could use sprintf.
See more at sprintf - C++ ReferenceCode:float myFloat = 3.23; char myString[30]; sprintf(myString, "%f", myFloat);
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.
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"
That does not make it part of the C standard library, which is what "standard" means in this forum with respect to a function.Originally Posted by stahta01
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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%.
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.
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"
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...