-2

(the problem is caused by me and has been solved ,greetings from the newbie)I apologize to everyone, my function type was integer i just realized it, I opened it because I worked for hours, I have to delete it). I am using gcc 9.3.0 version and standard settings. In the code in the example, I am getting 7 while waiting to get output 7.14. What could be the reason for this?(im using gcc main.c -lm -o main when compiling,what I'm trying to do is print as an integer if the double number is equal to integer, otherwise print it as a double,sorry for the adjustments, this is the last and proper version)

#include <stdio.h>
#include <math.h>

int try(double a);

int main() {
  double b = 7.14;
  double x;
  double temp;

  x = try(b);

  if (fmod(x, 1) == 0.0) {  // print 7 in this function
    temp = x;
    printf("%d", (int)temp);
  } 
   else if (fmod(x, 1) != 0.0) {
    printf("%f", x);
  }

  return 0;
}

int try(double a) {
  if (fmod(a, 1) != 0.0) {
    printf("%lf", a);  // print 7.14 in this function
  } else if (fmod(a, 1) == 0.0) {
    printf("%d", (int)a);
  }

  return a + 0;
}
10
  • 1
    I can't reproduce; prints out 7.140000 for me. Commented Mar 24, 2021 at 1:51
  • 1
    Also, it's a bad idea to compare a double with ==. Instead use <= delta where delta is some small value. Commented Mar 24, 2021 at 1:52
  • I use the double value returned from the other function instead of x, when I print it in the other function, it prints it as 7.14 but when i do this in main function it print 7.0.I immediately edit the question to avoid confusion Commented Mar 24, 2021 at 1:57
  • I get 7.140000 with gcc 9.3.0. wandbox.org/permlink/xipjKx9PUx6fgox0 Same after the edit: wandbox.org/permlink/UMHioIMbBkvawokq Commented Mar 24, 2021 at 2:00
  • 1
    I believe the printf format specifier for double should be just %f and not %lf. (It's different from scanf.) Does that change anything? Commented Mar 24, 2021 at 2:02

2 Answers 2

0

With gcc version 10.2.0 (GCC)

Got the result 7.140000

Some code-format and debug output.

#include <math.h>
#include <stdio.h>

double func_try(double a) { return a + 0; }

int main() {
  double b = 7.14;
  double x;
  x = func_try(b);

  printf("%lf\n", fmod(x, 1));

  if (fmod(x, 1) != 0.0) {
    printf("%lf", x);
  } else if (fmod(x, 1) == 0.0) {
    printf("%d", (int)x);
  }

  return 0;
}
$gcc main.c -lm -o main

$./main
0.140000
7.140000
Sign up to request clarification or add additional context in comments.

Comments

0

fmod(7.14, 1) = 0.14, thus it hit the first if and print 7.14.

Compute remainder of division

Returns the floating-point remainder of numer/denom (rounded towards zero):

fmod = numer - tquot * denom

Where tquot is the truncated (i.e., rounded towards zero) result of: numer/denom.

http://www.cplusplus.com/reference/cmath/fmod/

1 Comment

OP says they're getting 7, not 7.14, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.