I can print digits with putchar but how to print floats with put char?

This is a discussion on I can print digits with putchar but how to print floats with put char? within the C Programming forums, part of the General Programming Boards category; Hello So the function below can print any int using putchar. But I can't figure out how to print floats ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    I can print digits with putchar but how to print floats with put char?

    Hello

    So the function below can print any int using putchar. But I can't figure out how to print floats using putchar. Any ideas on this?

    Regards!

    Code:
    void putDigit(int n){
      int pow = 1;
      char lastChar = ' ';
      if (n < 0){
        putchar('-');
        lastChar = (char)((int)'0' + (n % 10));
        n /= 10;
        n *= (-1);
      }
      while (pow * 10 <= n)
        pow *= 10;
      while (pow != 0){
        int d = n / pow;
        putchar((char)((int)'0' + d));
        n = n - d * pow;
        pow /= 10;
      }
      if (lastChar != ' '){
        putchar(lastChar);
      }
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Dear Friend,

    You can't get the floating value correctly. Because in the putDigit function you gave the argument type as int. So if we pass the floating point value , it will convert the float into int. So it will take the number before "." only.

    If we change the argument type as float also, it will give the error. Because we can't perform the % operation for the floating point value.

    Thank you,

  3. #3
    Registered User ungalnanban's Avatar
    Join Date
    Feb 2010
    Location
    Chenai
    Posts
    12

    Thumbs up floating value is not possible

    you can print the integer values using your function. you cannot print the floating values.
    because.

    [%] modules operations are only allowed for integer. see in your code the following line
    Code:
    lastChar = (char)((int)'0' + (n % 10));
    The negative number's is not possible your code.
    ================================================== ========================
    you can handling the '.' dot in your code.


    see the following code in this code I am handling [.] floating values
    Code:
    double atof(char s[])
    {
    double val, power;
    int i, sign;
    
    for(i=0;isspace(s[i]); i++)// skip white space
            ;
    sign = (s[i] == '-') ? -1 : 1;
    if (s[i] == '+' || s[i] == '-')
    i++;
    
    for (val =0.0; isdigit(s[i]);i++)
    val = 10.0 * val + (s[i]-'0');
    
    if (s[i] == '.')
    i++;
    
    for(power = 1.0; isdigit(s[i]); i++)
    {
    val = 10.0 * val + (s[i]-'0');
    power *=10.0;
    //printf("val:%d",val);
    }
    return sign * val/ power;
    }
    Last edited by ungalnanban; 02-17-2010 at 11:54 PM. Reason: some spell mistakes

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Here's one way (not necessarily the best, but very intuitive), which I will describe to you in an example and let you do the coding.
    Say our number is 6543.21
    I take the log10 of the number rounded down, which is 3.
    I divide the number by 10^3 and round down, which is 6.
    I print out the 6.
    subtract 6x10^3 from the number. Now the number is 543.21
    I take the log of the number rounded down: 2
    and so on.
    keep doing this until you have printed out the number of digits that you want.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Quote Originally Posted by murugaperumal View Post
    Dear Friend,

    You can't get the floating value correctly. Because in the putDigit function you gave the argument type as int. So if we pass the floating point value , it will convert the float into int. So it will take the number before "." only.

    If we change the argument type as float also, it will give the error. Because we can't perform the % operation for the floating point value.

    Thank you,
    Thanks so much for clearing that up, Murugaperumal.

    The first step is to study and understand the floating-point specs for your particular machine (typically IEEE 754). Short of that, you can always fall back on ssprintf or similar, of course, tho I'm assuming that the purpose of this exercise is to do everything from scratch, correct?

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Quote Originally Posted by NeonBlack View Post
    Here's one way (not necessarily the best, but very intuitive), which I will describe to you in an example and let you do the coding.
    Say our number is 6543.21
    I take the log10 of the number rounded down, which is 3.
    I divide the number by 10^3 and round down, which is 6.
    I print out the 6.
    subtract 6x10^3 from the number. Now the number is 543.21
    I take the log of the number rounded down: 2
    and so on.
    keep doing this until you have printed out the number of digits that you want.
    what do you mean when you say "I divide the number by 10^3 and round down, which is 6." you divide 3 by 1000 ?

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    6543.21/1000 = 6.54321
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    I'm having some trouble making this work. Any one want to give this a try?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void putcf( float f )
    {
        char buf[ BUFSIZ ] = {0};
        size_t x;
    
        for( x = 0, sprintf( buf, "%f", f ); x < strlen( buf ); x++ )
            putchar( buf[ x ] );
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Quote Originally Posted by NeonBlack View Post
    6543.21/1000 = 6.54321
    Here is what I coded it and it prints everything upto the decimal point.

    Code:
    void putFloat(double n){
      double x;
      int r, second;
      x = log10(n);
      r = n/1000;
      putDigit(r);
      second = n - (6*1000);
      putDigit(second);
        double x;
      int r, second;
      x = log10(n);
      r = n/1000;
      putDigit(r);
      second = n - (6*1000);
      putDigit(second);
    
    
    }
    But how can I print the dot and everything after that now?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No offense, but you've really taken a wrong turn on this, imo.

    Take what you have up above this post, and toss it - and the farther the the better. Look carefully at what Quzah has posted above. Despite the "little surprise" he's placed in it, that is how it should be done.

    It's clear, direct, concise.

    What more could you ask for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM

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