I was trying to extract the exact fractional part from a floating point number. I tried with this:
float f=254.73;
int integer = (int)f;
float fractional = f-integer;
printf ("The fractional part is: %f", fractional);
But the output is: 0.729996
. For this reason when I was doing this:
float f=254.73;
int integer = (int)f;
float fractional = f-integer;
int fractional_part_in_integer = ((int)(f*100)%100);
printf ("The value is: %d", fractional_part_in_integer);
It gives me 72
as output. But, I want to extract exactly 73
from the given number 254.73
. I already know how to use %.2f
during printf()
function to print upto two decimal numbers. But in my code I don't want to print the number right now. I have some calculations with that fractional part as integer form i.e. 73
.
So, my problem is how could I extract the fractional part from 254.73
so that I can get exact 73
as integer to do more calculations?
Using the modulo ( % ) operator The % operator is an arithmetic operator that calculates and returns the remainder after the division of two numbers. If a number is divided by 1, the remainder will be the fractional part. So, using the modulo operator will give the fractional part of a float.
Method 1: Conversion using int(): To convert a float value to int we make use of the built-in int() function, this function trims the values after the decimal point and returns only the integer/whole number part.
For example, one could multiply it by 100 (so that 21.83 becomes 2183), then find the remainder on division by 100 (so that 2183 becomes 83), then divide by 100 (so that 83 becomes 0.83). Or, if what you have is the floor function, you could find x−⌊x⌋, for example: 21.83−⌊21.83⌋=0.83.
Cast to an int value: When a float value is cast to an int value, it loses the fractional part of the number, regardless of the value. $int_value = (int) 2.87; // $int_value = 2; Notice how the . 87 is lost when 2.87 is cast to an int .
trying to extract the exact fractional part from a floating point number. double modf (double value, double *iptr); float modff (float value, float *iptr); The modf functions break the argument value into integral and fractional parts, ... #include <math.h> double value = 1.234; double ipart; double frac = modf (value, &ipart);
modf is an inbuilt function defined in math.h header file. We can use this function to convert a floating point variable to integer and fractional or decimal part. v is the floating-point value we want to divide into fractional and decimal parts.
My current way of getting the fraction is to convert the float to fixed point (multiply with (float)0xFFFF, cast to int), get only the lower part (mask with 0xFFFF) and convert it back to a float again. However, the imprecision is killing me. I'm using my Frac () and InvFrac () functions to draw an anti-aliased line.
Our program will take the floating-point number as an input from the user and it will print the integer part and the fraction part. We can solve this problem in different ways in C. Let’s have a look at them one by one. In this method, we can cast the number to an integer by using (int) typecasting.
How to get the exact fractional part from a floating point number as an integer?
trying to extract the exact fractional part from a floating point number.
Use modf()
or modff()
double modf(double value, double *iptr);
float modff(float value, float *iptr);
The
modf
functions break the argument value into integral and fractional parts, ...
C11 §7.12.6.12 2
#include <math.h>
double value = 1.234;
double ipart;
double frac = modf(value, &ipart);
A better approach for OP's need may be to first round a scaled value and then back into whole and fractional parts.
double value = 254.73;
value = round(value*100.0);
double frac = fmod(value, 100); // fmod computes the floating-point remainder of x/y.
double ipart = (value - frac)/100.0;
printf("%f %f\n", ipart, frac);
254.000000 73.000000
Ref detail: When OP uses 254.73
, this is converted to the nearest float
value which may be 254.729995727539...
.
float f = 254.73;
printf("%.30f\n", f);
// 254.729995727539062500000000000000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With