Question:
C Programming - Extracting a significant digit from a floating-point?
tournesol
2010-02-23 22:55:30 UTC
Write a C program that will ask for a floating-point value and then display the ten-digit of the integral part and the second most significant digit of the fractional (decimal) part.
The program should display the output to screen as
Enter a floating-point + [ENTER] : 135.246
The ten digit of the integral part: 3
The second most significant digit of fractional part: 4
Enter another floating-point + [ENTER] : 296.135
The ten digit of the integral part: 9
The second most significant digit of fractional part: 3

I know how to use printf and scanf... For scanf I had the input stored as floating-points. I know how to find the 10-digit integral part from integers, but I don't know how to extract it from a floating point. Help please!
Three answers:
cja
2010-02-24 05:08:32 UTC
Working with floating point numbers in software is not an exact science, so inaccuracy can creep in making this problem more difficult. See below for how I did it. The comments in the code will explain.



#include



#define MAX_LINE_LEN 80



char line[MAX_LINE_LEN];



int main(int argc, char *argvp[]) {

    float n,x;

    int i,j;



    printf("Enter real numbers:\n");

    while (1) {

        printf("> ");

        fgets(line,MAX_LINE_LEN,stdin);

        if (sscanf(line,"%f",&n) == 1) {

            /*

              * Get 10s digit of whole part

              */

            i = ((int)n / 10) % 10;



            /*

              * Subtract whole part from original number,

              * and add a small delta to account for floating

              * point inaccuracy. This will restore the 2nd

              * most significant digit of the fractional

              * part if it was changed in the computation.

              */

            x = n - (int)n + 0.00005;



            /*

              * Multiply by 100 to get the 2nd most

              * significant digit of the fractional part

              * into the 1s digit of the whole part.

              */

            x *= 100;



            /*

              * Get the 1s digit.

              */

            j = (int)x % 10;



            /*

              * Display

              */

            printf("%d, %d\n",i,j);

        }

    }

    return 0;

}



#if 0



Sample run:



Enter real numbers:

> 123.456

2, 5

>

> xx.yz

> 45.67

4, 7

> 12.0099

1, 0

> 5678

7, 0

>



#endif
iammisc
2010-02-23 23:08:12 UTC
Personally, since your problem is pretty simple and pretty well-defined, I wouldn't read the number in as floating point. Just read it in as a string, mark the position of the "." sign using strchr, and do computations from there, using simple string indexing to get the number.
?
2016-11-09 04:30:58 UTC
if (a == b) is somewhat like like it seems, it would be equivalent bit by employing bit. You look over thinking the area. reckoning on the equation which you enter in C, no longer something is stoping you from specifying the enter variety. it fairly relies upon on what your doing.. it would desire to be a count of an person-friendly math operation. working example, you are able to positioned if (a > b), if (a < b), or you are able to subtract and notice in case you have a lot value left interior the constructive or adverse.. it fairly relies upon on what your doing. How the if assertion is performed could advance right into a lot clearer in case you knew basically slightly of undemanding assembly language. maximum computer processors are x86 based recently, so i'm going to offer you an occasion in x86 assembly code of how that could look. if (a == b) { } could appear like mov [variable a, eax] //flow variable a into sign up eax cmp [eax, variable b] //learn eax to the variable b jne [memory handle] // leap based the equivalent of the if assertion if eax isn't equivalent to variable b (in this subject evaluating variable a to variable b) i think my element is you will understand issues greater constructive in case you already know somewhat of what is going below. C provides you multiple means, its basically as much as you to make your techniques up mathematically the thank you to harness it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...