1. How would you know how many digits are there after decimal, or how many times to multiply by ten. 2. Originally Posted by GReaper 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! No, it's really not that simple.
123.456 cannot be represented exactly so it would be difficult to know when to stop outputting digits.
Try writing code to output 123.456e+34 as a string. Then try outputting 1.0/3.0.
Then take a value in a float that was not directly set from a constant but rather is the result of some calculation. Try and output that in such a way that when you convert from string back to float that the value is bit for bit equivalent to what it was originally. 3. Here's my version of ftoa(). It works fine, but when I provide some input, say, 23.23, it outputs 23.22999952....
and the problem is with line no.15. (i=i-j).
Befor the program reaches this line the value in i is 23.23 and that in j is 0.23.
But after the subtraction (i-j), for some reasons, i holds value 0.22999952.

Code:
```#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

char *iascii(int);

char* fascii(float i)         /* Function to convert a floating point number into a stream of ASCII characters */
{
char *s;int j,k;
float a,b,c;
j=i;
s=(char*)malloc(10*sizeof(char));
s=iascii(j);
printf("%s.",s);
i=i-j;

for(a=0,c=0.1,k=1;(b=(i-a))!=0;a+=c)
{
if(b<0)
{
b=i+a;
k++; a=c/10; c=a;
}
if(a==1)
{ a=c/10;c=a;}
}

for(;k>0;k--)
{
i=10*i;
}
j=i;
s=iascii(j);
return s;
}

char* iascii(int i)              /* Function to convert an integer into a stream of ASCII charcters */
{
char *s;
int j,k;
s=(char*)malloc(10*sizeof(char));
for(j=0;i!=0;j++)
{
*s++=(i%10)+48;
i=i/10;
}*s='\0';

for(k=j;k>0;k--,s--);
j--;
for(i=0;(j-i)>0;j--,i++)
{
char c;         /* Loop to reverse the string s */
c=s[i];
s[i]=s[j];
s[j]=c;
}

return s;
}

int main()
{
char *s;
float i;
puts("Enter any positive floating point number");
scanf("%f",&i);
s=fascii(i);
printf("%s",s);
}``` 4. Originally Posted by iMalc No, it's really not that simple.
123.456 cannot be represented exactly so it would be difficult to know when to stop outputting digits.
Try writing code to output 123.456e+34 as a string. Then try outputting 1.0/3.0.
Then take a value in a float that was not directly set from a constant but rather is the result of some calculation. Try and output that in such a way that when you convert from string back to float that the value is bit for bit equivalent to what it was originally.
Almost never the standard functions print floating point numbers as bit by bit equivalent of their binary types. They limit it to a certain amount of digits and fraction digits, and print until that limit is reached. You were right about the exponent, but I thought that the OP wanted his function to do as "printf("%f", someValue);" does. 5. Try to limit your outputted faction. Say you want to print six fraction digits, first add 0.0000005 to the number to round it up and then print the first six fraction digits. 6. First, Google for "What every computer scientist shoud know about floating point".
Read that if you have not already done so. It explains how floating point is only an approximation.

Second, you've been mislead, by someone who likely hasn't even gone as far as you have, into thinking that what you want to do is remotely easy. On the surface this problem really does look dead easy, but I assure you that it is not. A typical working solution is likely to be on the order of over a thousand lines of code. Anyone can code up a solution that will work for a very limited range of cases (fixed number of decimal places less than 10, whole number part not exceeding 10 digits, no infs or nans, results rounded i.e. the conversion is a one-way trip) but as you can see, even doing that is not a piece of cake.

If you're seriously considering writing the general version of this then you need to go and do some research first. I know it's hard because I have researched it. Unfortunately researching it is also kinda hard also because the kind of things you're likely to search for will tend to point you at the existing functions like ftoa. But with some creative searching, you may be able to find the stuff I came across some time ago.
I also recommend that if you want to go ahead then you will need to get (or build) a unit test framework and ideally work on this project using Test Driven Development (TDD) as it will save you time in the long run. You'll learn a lot, but that's about all you'll do because the whole time you'll be solving problems that have already been solved better by someone else. teideal glic deisbhéalach » Blog Archive » Here be dragons: advances in problems you didn’t even know you had
There was a breakthrough in this field just last year, which must have been when I read about it.
GReaper, you should probably have a read too.

Algorithm to convert an IEE 754 double to a string? - Stack Overflow
floating point - Exact textual representation of an IEEE "double" - Stack Overflow 8. Hmm....so what you are saying is that, "for understanding" we can't always abstract the language away from the hardware....  9. You trumbled me there, I didn't know there were so many problems for implementing such a function. Originally Posted by iMalc Second, you've been mislead, by someone who likely hasn't even gone as far as you have, into thinking that what you want to do is remotely easy.
Nice, iMalc. That's the thanks and recognision I get after spending so much time in these forums. You want a race? I've never been concerned with floating point number printing, so I really couldn't know better, but your attitude is way insulting!  10. I'm sorry. I went overboard this time, or at the very least worded things terribly.
You're an invaluable contributor to these forums. Popular pages Recent additions 