0

I'm trying to run this. I have tried fmod too. Nothing worked yet.

void main(){
    double a = 123.46566662;
    double frac = a - (long)a;
    printf("%f", frac);
}
//tried int64_t too   
// output is :.465667
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
sociopath
  • 79
  • 1
  • 6

2 Answers2

0

Use .10f or .nf precision to get the desired precision. here n = number of precision. thanks to @StoryTeller

void main()  
{      
double a=123.46566662;  
double frac=a-(long)a;  
printf("%.10f",frac);  
} 

//output :.4656666200
msc
  • 33,420
  • 29
  • 119
  • 214
sociopath
  • 79
  • 1
  • 6
0

The idea of "exact precision" is what gets your. Decimal fractions (generally) cannot be represented in a finite binary number. Much like there's no "exact" decimal fraction for a 2/3.

The best you can hope for is to have a "close enough" fractional part with an ever-higher precision based on your data type (think float vs. double).

If you do indeed have to get the "exact precision" I recommend you implement your own "precise" arithmetic, in which you store decimal numbers in decimal representation. Be it a string, a BCD, or an array of digits.

YePhIcK
  • 5,816
  • 2
  • 27
  • 52