0

I am looking for a way to compare a decimal values first decimal point. (-1.064728) In my project I need to know if this decimal is 0 or not, but I don't know how to do that. Can somebody help me please.

Unknown
  • 71
  • 8
  • 1
    Hi! Welcome to StackOverflow! What have you considered doing so far? – norok2 Feb 01 '19 at 08:52
  • Step 1: [get the fractional part](https://stackoverflow.com/questions/2594913/getting-the-fractional-part-of-a-float-without-using-modf) – Duck Dodgers Feb 01 '19 at 08:56
  • How is your number stored? Is it currently a character array or numeric value (e.g. `double` or `float`)? – David C. Rankin Feb 01 '19 at 08:56
  • Step 2; [extract the digits from the number](https://stackoverflow.com/questions/1397737/how-to-get-the-digits-of-a-number-without-converting-it-to-a-string-char-array) – Duck Dodgers Feb 01 '19 at 08:57
  • This looks like a math question. – Jose Feb 01 '19 at 08:58
  • @DavidC.Rankin it's a double – Unknown Feb 01 '19 at 09:00
  • See [modf(3) - Linux manual page](http://man7.org/linux/man-pages/man3/modf.3.html) -- the 3 variants. – David C. Rankin Feb 01 '19 at 09:04
  • @Jose Although math is present, it is the math of `double` and not the math of _real numbers_ that makes this question very appropriate to SO. – chux - Reinstate Monica Feb 01 '19 at 14:25
  • @chux I am agree that the question involves the math of `double`, but my comment was more focused on the idea that the OP does not include any progress beforehand. There are no a)an algorithm, b)an algorithm to pseu-code conversion, c)a draft of the code (which includes the math of `double`) d)and after those steps, the SO question. – Jose Feb 01 '19 at 14:36

3 Answers3

4

This is the simplest way to approach this problem for beginner. My solution uses modulus operator which calculates reminder after a division.

So you multiply number by 10 which means you push decimal point one position right. This means you get -10.64728. After that explicitly convert number to integer, which removes decimal part of the number and you get -10.

Then you use modulus operator which divides the given number by 10 and returns the remainder of division, which is in this case  0.

#include <stdio.h>

int main()
{
    float con = -1.064728;
    
    // This will put first decimal as last digit and remove all decimals
    int res = (int) (con * 10); 
    
    // Take last digit of integer
    res = res % 10; 
    
    if (res == 0) {
        printf("Result is zero");
    } else {
        printf("Result is %d", res);
    }

    return 0;
}

For numbers greater than (2^31-1)/10

Also if you need to work with number greater then tenth of a integer's max range, I recommend using number in string form. Where simplest solution would be this.

#include <stdio.h>

int main()
{
    char* c = "-1151651651651651781651.064728";
    
    char result = '\0';
    
    int i = 0;
    // Check char on position `i`
    while(c[i] != 0) {
        // Check if current number is decimal delimiter
        // Also check if next char is not end of string
        
        if (c[i] == '.' && c[i+1] != 0) {
            // Write next character as result and exit loop
            
            result = c[i+1];
            break;
        }
        
        // Increase iterator
        i++;
    }
    
    if (result == '0') {
        printf("Result is zero");
    } else {
        printf("Result is %c", result);
    }

    return 0;
}

If you really want to optimise your code in the second case, I would recommend getting rid of iterator integer and tried abusing the pointer.

Po1nt
  • 522
  • 1
  • 4
  • 13
  • 1
    Be very careful, the range of `float` can exceed the range of `int` by many orders of magnitude. – David C. Rankin Feb 01 '19 at 09:06
  • @DavidC.Rankin This is actually a good point. Be careful with numbers higher then (2^31-1)/10. As they overflow the integer and the code will no longer work properly. – Po1nt Feb 01 '19 at 09:10
  • I've added solution for bigger numbers. – Po1nt Feb 01 '19 at 09:27
  • The "recommend using number in string form." is reasonable alternative, yet skips on how to properly take the `double` into sting form. Without sufficient precision in that, again, rounding problems occur as well a infinity/NaN. – chux - Reinstate Monica Feb 01 '19 at 14:28
  • Re: "For numbers greater then (2^31-1)/10", instead consider `long long` with its 64+ bit math. For common `double`, all `double > 2^53` have a _first decimal point_ of 0, so only smaller values (in range of `long long`) need further processing. – chux - Reinstate Monica Feb 01 '19 at 14:30
  • Detail: C does not call `%` the _modulus operator_, but the _remainder_. That [difference](https://stackoverflow.com/a/20638659/2410359) is sometimes functionally important - though not so much here. – chux - Reinstate Monica Feb 01 '19 at 14:34
  • The first approach is weak for values near xxx.x9999... as the con * 10 can readily incur rounding and then alter the sought after digit. Thus the conversion to a whole number (int) is based on the rounded value and not the original one. – chux - Reinstate Monica Feb 01 '19 at 14:37
  • I would like to pinpoint that this was supposed to be simplest solution for beginner, not the optimal one. I think beginner can easily swap int for long and get rid of the problem, but the character solution is suitable for practicaly unlimited digits. – Po1nt Feb 01 '19 at 15:08
1
double fraction = number - trunc(number);
int digit = 10 * fraction;
if (digit) { ... }
else { ... }
user7048748
  • 139
  • 7
0
float whole = floor(n);
float dec = n - whole;
if( dec == 0 ) { /*if decimal is zero*/}
else { /*if decimal is not zero*/}
Mayur
  • 2,583
  • 16
  • 28