For example if I input 123.45678 I get in output 123 and 0.456779 instead of 0.45678, can anyone explain why is that so? I though it might be something with float data type, but I need a comprehensive explanation, thank you:)
the code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void decompose(float number, float *decimal, int *whole);
int main()
{
float number, decimal;
int whole;
printf("\n=== Float Decomposition ===\n\n");
printf("Enter the number you would like to decompose: ");
scanf("%f",&number);
printf("\n");
decompose(number, &decimal, &whole);
printf("The whole part of your number is: \n");
printf("%d\n",whole);
printf("fractional part: \n");
printf("%f\n",decimal);
return EXIT_SUCCESS;
}
void decompose(float number, float *decimal, int *whole){
*decimal = number - trunc(number);
*whole = number - *decimal;
return;
}