Like Tree4Likes

Float to ASCII

This is a discussion on Float to ASCII within the C Programming forums, part of the General Programming Boards category; How would you know how many digits are there after decimal, or how many times to multiply by ten....

  1. #16
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    How would you know how many digits are there after decimal, or how many times to multiply by ten.

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by GReaper View Post
    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.
    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"

  3. #18
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    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);
    }
    Last edited by juice; 10-02-2011 at 10:31 PM.

  4. #19
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by iMalc View Post
    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.
    Devoted my life to programming...

  5. #20
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    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.
    Last edited by GReaper; 10-03-2011 at 12:31 AM.
    Devoted my life to programming...

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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.
    Salem and juice like this.
    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"

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Alright, after much searching, here is the link that I originally read about this problem:
    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.

    Other links:
    Algorithm to convert an IEE 754 double to a string? - Stack Overflow
    floating point - Exact textual representation of an IEEE "double" - Stack Overflow
    Salem and juice like this.
    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. #23
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Hmm....so what you are saying is that, "for understanding" we can't always abstract the language away from the hardware....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #24
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    You trumbled me there, I didn't know there were so many problems for implementing such a function.

    Quote Originally Posted by iMalc View Post
    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!
    Devoted my life to programming...

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    I'm sorry. I went overboard this time, or at the very least worded things terribly.
    You're an invaluable contributor to these forums.
    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"

Page 2 of 2 FirstFirst 12
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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21