Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello guys.I tried to take mod of float number to understand it's an integer or not.I want to print float number as integer if it doesn't have any remainder.I mean if result is "4.00(float)" I want to print "4".On the other hand,if it is a number like 1.50 I want to print same again.
I guess there is a function in math.h library "modf" to take remainder of x/y but it doesn't works :(
(other parts works without any problem)




C++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float operation(float num1,char op,float num2)
{
   float res;
   float c=modf(res,1);//Error is here.I tried to take remaining from result/1.
   if(op=='+')
   {
      res=num1+num2;
   }
   else if(op=='-')
   {
      res=num1-num2;
   }
   else if(op=='*')
   {
      res=num1*num2;
   }
   else if(op=='/')
   {
      res=num1/num2;
   }
   else
   {
      printf("Error!Something wrong about operator.");
   }
   if(c==0)
   {
      printf("=%0.2f",res);//if remainder=0 that means it's an integer.
   }
   else if(c!=0)
   {
      printf("%d",(int)res);//if remainder is not 0 that means it's a float.
   }
}


main()
{
   printf("Welcome to Basic Calculator---Made by Cevik Cukurova\n");
Start:
   int dec;
   float num1,num2;
   char op;
   printf("Please enter the operation.\n ");
   scanf("%f",&num1);
   scanf("%c",&op);
   scanf("%f",&num2);
   operation(num1,op,num2);
   printf("\n Press 1 to enter another operation.");
   scanf("%d",&dec);
   if(dec==1)
   {
      goto Start;
   }
   return 0;
}


What I have tried:

As I said I already tried modf function.
Posted
Updated 16-Feb-20 7:20am
v2
Comments
Richard MacCutchan 16-Feb-20 7:24am    
When you receive an error it really makes sense to explain exactly what that error is.
KarstenK 17-Feb-20 12:35pm    
Put more user information about input and operator to the screen.

I would use "space" for another operation ;-)

The modf function is documented here:
C library function - modf() - Tutorialspoint[^]
The second parameter is not the divisor, it should be a pointer to a variable of the same type as the first argument.
Another way to get the decimal part of a number is to subtract its integer part from it.
C++
double value1 = 4.5;
double value2 = 3;
double dec1 = value1 - floor(value1);
double dec2 = value2 - floor(value2);
assert(dec1 != 0);
assert(dec2 == 0);

But your issue could as well be as simple as formatting the value (no need to do any prior computation at all). Maybe Format Specification Syntax: printf and wprintf Functions | Microsoft Docs[^] can help you, then.
 
Share this answer
 
v2
Comments
k5054 16-Feb-20 11:40am    
The link posted here is for C++ formatting, while the program is in C, and uses printf(). Google can help find a tutorial, if the OP needs it.
phil.o 16-Feb-20 12:02pm    
Good point. I updated the links.
Thanks.
Try
C
#include <stdio.h>
#include <math.h>
#include <assert.h>

int good_operator(char op);
double operation(double num1 ,char op, double num2);

int main()
{
  const double EPSILON = 0.001;

  printf("Welcome to Basic Calculator---Made by Cevik Cukurova\n");

  int cont;

  do
  {

    double num1,num2, result, intpart;
    char op;
    printf("Please enter the operation.\n ");
    scanf("%lf",&num1);
    scanf("%c",&op);
    scanf("%lf",&num2);
    if ( ! good_operator(op) )
    {
      printf("Error! Something wrong about operator.\n");
    }
    else
    {
      result = operation(num1, op, num2);
      printf("result = ");
      if ( modf(result, &intpart) > EPSILON )
      {
        printf("%g\n", result);
      }
      else
      {
        printf("%d\n", (int)intpart);
      }
    }
    printf("\n Press 1 to enter another operation.\n");
    scanf("%d",&cont);
  } while (cont ==1);

  return 0;
}

int good_operator(char op)
{
  return (op == '+' || op == '-' || op == '*' || op == '/');
}

double operation(double num1 ,char op, double num2)
{
  double result;

  if (op == '+')
  {
    result = num1+num2;
  }
  else if (op == '-')
  {
    result = num1-num2;
  }
  else if (op == '*')
  {
    result = num1*num2;
  }
  else if( op =='/')
  {
    result = num1/num2;
  }
  else
  {
    assert(0);
  }
  return result;
}
 
Share this answer
 
Comments
Jaqross 17-Feb-20 8:42am    
Thanks,it works well...But i couldn't understand what is epsilon :)
CPallini 17-Feb-20 9:02am    
It is a small (arbitrary, of your choice) value used to compare doubles in a 'commonsense way', that is, if
(a - b) > EPSILON

then a is (about) equal to b (i.e. there is a small difference between the two numbers).
See, for instance:
https://floating-point-gui.de/errors/comparison/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900