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
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
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.