Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of digits after `.` in floating point numbers?

This is one interview question. How do you compute the number of digit after . in floating point number.

e.g. if given 3.554 output=3

for 43.000 output=0. My code snippet is here

double no =3.44;
int count =0;
while(no!=((int)no))
{
    count++;
    no=no*10;
}
printf("%d",count);

There are some numbers that can not be indicated by float type. for example, there is no 73.487 in float type, the number indicated by float in c is 73.486999999999995 to approximate it.

Now how to solve it as it is going in some infinite loop.

Note : In the IEEE 754 Specifications, a 32 bit float is divided as 24+7+1 bits. The 7 bits indicate the mantissa.

like image 549
someone Avatar asked Jul 24 '13 14:07

someone


People also ask

How many digits are present after the decimal in float value?

How many digits are present after the decimal in float value? Explanation: None.

How do you find the digits after the decimal point?

If a number has a decimal point , then the first digit to the right of the decimal point indicates the number of tenths. For example, the decimal 0.3 is the same as the fraction 310 . The second digit to the right of the decimal point indicates the number of hundredths.

How do I count the number of digits after a decimal point in Excel?

Count and identify the decimal place of a number You can do it with following steps: In a blank cell, saying the Cell B2, enter the formula of =IF(A2=INT(A2),0,LEN(MID(A2-INT(A2),FIND(".",A2,1),LEN(A2)-FIND(".",A2,1)))), and press the Enter key. Then it returns the decimal place in the Cell B2.


5 Answers

I doubt this is what you want since the question is asking for something that's not usually meaningful with floating point numbers, but here is the answer:

int digits_after_decimal_point(double x)
{
    int i;
    for (i=0; x!=rint(x); x+=x, i++);
    return i;
}
like image 113
R.. GitHub STOP HELPING ICE Avatar answered Nov 15 '22 06:11

R.. GitHub STOP HELPING ICE


The problem isn't really solvable as stated, since floating-point is typically represented in binary, not in decimal. As you say, many (in fact most) decimal numbers are not exactly representable in floating-point.

On the other hand, all numbers that are exactly representable in binary floating-point are decimals with a finite number of digits -- but that's not particularly useful if you want a result of 2 for 3.44.

When I run your code snippet, it says that 3.44 has 2 digits after the decimal point -- because 3.44 * 10.0 * 10.0 just happens to yield exactly 344.0. That might not happen for another number like, say, 3.43 (I haven't tried it).

When I try it with 1.0/3.0, it goes into an infinite loop. Adding some printfs shows that no becomes exactly 33333333333333324.0 after 17 iterations -- but that number is too big to be represented as an int (at least on my system), and converting it to int has undefined behavior.

And for large numbers, repeatedly multiplying by 10 will inevitably give you a floating-point overflow. There are ways to avoid that, but they don't solve the other problems.

If you store the value 3.44 in a double object, the actual value stored (at least on my system) is exactly 3.439999999999999946709294817992486059665679931640625, which has 51 decimal digits in its fractional part. Suppose you really want to compute the number of decimal digits after the point in 3.439999999999999946709294817992486059665679931640625. Since 3.44 and 3.439999999999999946709294817992486059665679931640625 are effectively the same number, there's no way for any C function to distinguish between them and know whether it should return 2 or 51 (or 50 if you meant 3.43999999999999994670929481799248605966567993164062, or ...).

You could probably detect that the stored value is "close enough" to 3.44, but that makes it a much more complex problem -- and it loses the ability to determine the number of decimal digits in the fractional part of 3.439999999999999946709294817992486059665679931640625.

The question is meaningful only if the number you're given is stored in some format that can actually represent decimal fractions (such as a string), or if you add some complex requirement for determining which decimal fraction a given binary approximation is meant to represent.

There's probably a reasonable way to do the latter by looking for the unique decimal fraction whose nearest approximation in the given floating-point type is the given binary floating-point number.

like image 22
Keith Thompson Avatar answered Nov 15 '22 05:11

Keith Thompson


The question could be interpreted as such:

Given a floating point number, find the shortest decimal representation that would be re-interpreted as the same floating point value with correct rounding.

Once formulated like this, the answer is Yes we can - see this algorithm:

Printing floating point numbers quickly and accurately. Robert G. Burger and R. Kent Dybvig. ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation, June 1996

http://www.cs.indiana.edu/~dyb/pubs/FP-Printing-PLDI96.pdf

See also references from Compute the double value nearest preferred decimal result for a Smalltalk implementation.

like image 21
aka.nice Avatar answered Nov 15 '22 05:11

aka.nice


Sounds like you need to either use sprintf to get an actual rounded version, or have the input be a string (and not parsed to a float).

Either way, once you have a string version of the number, counting characters after the decimal should be trivial.

like image 25
Drew McGowen Avatar answered Nov 15 '22 06:11

Drew McGowen


It is my logic to count the number of digits. number = 245.98

  1. Take input as a string char str[10] = "245.98";
  2. Convert string to int using to count the number of digits before the decimal point. int atoi(const char *string)
  3. Use logic n/10 inside the while to count the numbers.
  4. Numbers after decimal logic
    • Get the length of the string using strlen(n)
    • inside the while (a[i]! ='.'). then increment i Later you can add step 3 logic output and step 4 logic output

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char num[100] = "345653.8768";
    int count=0;
    int i=0;
    int len;
    int before_decimal = atoi(num);
    int after_decimal;
    int total_Count;
    printf("Converting string to int : %d\n", before_decimal);
    
    //Lets count the numbers of digits before before_decimal
    while(before_decimal!=0){
        before_decimal = before_decimal/10;
        count++;
    }

    printf("number of digits before decimal are %d\n",count);
    //Lets get the number of digits after decimal
    
    // first get the lenght of the string
    len = strlen(num);
    printf("Total number of digits including '.' are =%d\n",len);
    
    //Now count the number after '.' decimal points
    // Hope you know how to compare the strings
    while(num[i]!='.'){
       i++; 
    }
    // total lenght of number - numberof digits after decimal -1(becuase every string ends with '\0')
    after_decimal= len-i-1;
    printf("Number of digits after decimal points are %d\n",after_decimal);
    
    //Lets add both count Now
    // ie. Number of digits before decmal and after decimal
    
    total_Count = count+ after_decimal;
    
    printf("Total number of digits are :%d\n",total_Count);
    return 0;
}

Output:

Converting string to int : 345653                                                                                                                                                  
number of digits before decimal are 6                                                                                                                                              
Total number of digits including '.' are =11                                                                                                                                       
Number of digits after decimal points are 4                                                                                                                                        
Total number of digits are :10   

                 
like image 31
Mahadev Gouda Avatar answered Nov 15 '22 06:11

Mahadev Gouda