Thread: how to read a digit of a floating point number?????

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    1

    Unhappy how to read a digit of a floating point number?????

    how do we read a particular digit/digits of a floating point number???

    ive jus started out with C....hardly know anything and i was presented with this problem....

    im supposed to write a program which accepts a floating point number from the user....and displays the rightmost digit of the integral part....(the digit at ones place)..

    next..it asked me to modify the program such that two od the rightmost digits of the integral part of the number were displayed...

    ill be very thankful for any help...

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I'd convert it to a string, read it until reaching a dot or null char then get the character before it.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Few ways of doing it:

      1. I think you can call floor() on the float, and then cast it to an int. This should take something like 34.54 and convert it to 34.
      2. Get the remainder of the int divided by 10 (Hint: % operator) 34 / 10 = 3, R = 4
      3. Print.

      1. Read in float as a string with fgets().
      2. Find the '.' char.
      3. If it exists, print the char before it.
      4. If it doesn't exist, print the last char of the string.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    1. I think you can call floor() on the float, and then cast it to an int. This should take something like 34.54 and convert it to 34.
    Would copying the float to an int not automatically floor it? Thats what I have been doing (i think).

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I think you're right, but for whatever reason I was under the impression the floor() was needed. I don't remember what prompted me to think that.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mike_g View Post
    Would copying the float to an int not automatically floor it? Thats what I have been doing (i think).
    Consider negative numbers.

    EDIT: Also, modf() is what you want.

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    The string method using sprintf() is probably the best. just read until you see the period or reach teh end of the string

    Code:
     
    void func(double input){
         char temp[256];
         int index;
     
         sprintf((char*)temp , "%f" , input);
         index =0;
         while((temp[index] != 0) && (index<256) && (temp[index] != '.')) index++;
         index--;
     
         // index now points at the ones place
     
     
         return;
         }

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could make your job easier with &#37;.0f:
    Code:
    double get_first_digit(double number) {
        char buffer[BUFSIZ];
    
        sprintf(buffer, "%.0f", number);
    
        if(*buffer) {
            return buffer[strlen(buffer) - 1] - '0';
        }
    
        return 0;
    }
    But anyway, using a string is a bad idea in my opinion. You use lots of extra memory, and sprintf()'s probably pretty slow too. The mathematical way isn't much harder, if any.

    I think you're right, but for whatever reason I was under the impression the floor() was needed. I don't remember what prompted me to think that.
    Casting a floating point number to an int would indeed have the same effect as flooring it. But using floor() and modf() instead of a cast to an integral type is a much better idea, because a floating point number can easily store values that cannot be represented in any integral type. If you casted one of those values to (int), you'd get an integer overflow.

    So floor() exists so that you can round a number without risk of overflow.

    Anyway, you could also use something like this:
    Code:
    double get_first_digit(double number) {
        return floor(floor(number/10)*10 - number);
    }
    That's probably a bad way to do it . . . perhaps something like this, then.
    Code:
    double get_first_digit(double number) {
        return floor(fmod(number, 10));
    }
    To get the first decimal digit, of course.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    Casting a floating point number to an int would indeed have the same effect as flooring it.
    Again though, not always. Consider a negative number. On my system, (int)(-3.5) == -3, not -4. A floor() operation would have taken that value to -4.0.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by brewbuck View Post
    Again though, not always. Consider a negative number. On my system, (int)(-3.5) == -3, not -4. A floor() operation would have taken that value to -4.0.
    Floor()ing the float under that condition is not what the OP wants I take it. Straight casting might be better if he chooses to do conversions to an int.

    The string method still sounds good. lol...

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    Floor()ing the float under that condition is not what the OP wants I take it. Straight casting might be better if he chooses to do conversions to an int.
    How about:

    Code:
    int last_digit_of_integral_part(float v)
    {
        if(v < 0.0) v = -v;
        return (int)((v * 0.1 - (int)(v * 0.1)) * 10.0);
    }

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I like
    Code:
    double get_first_digit(double number) {
        if(number < 0) {
            return ceil(fmod(number, 10));
        }
        else {
            return floor(fmod(number, 10));
        }
    }
    Or if the digit alone was required, without a sign:
    Code:
    floor(fmod(fabs(number), 10));
    [edit] The C99 round() would work too, replacing floor() and ceil().
    Code:
    round(fmod(number, 10));
    [/edit]
    Last edited by dwks; 07-13-2007 at 04:39 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Using floor explicitly also ensures correct operation if the /QIfist compiler option is turned on. (Which I currently do for my software 3D engine)
    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"

  14. #14
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    what about
    Code:
    printf("%1.0f\n",num);
    this has a width of 1 and since no decimals all the width will be in the ones place right?
    similarly
    Code:
    printf("%2.0f\n",num);

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	float fNum = 123.456;
    	
    	printf("fNum = &#37;1.0f\n",fNum);
    	
    	return 0;
    }
    Output:

    Code:
    fNum = 123
    In other words, no. That's wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number comparison
    By lehe in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2009, 07:11 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. floating point function
    By Sal79 in forum C++ Programming
    Replies: 17
    Last Post: 04-17-2007, 06:49 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM