Pg. 61 - // Pass the address of integerPart as an argument


I can’t figure out the logic of “fractionPart = modf(pi, &integerPart);”. Where pi is given a double of 3.14 I am assuming that double integerPart; and double fractionPart; are now known values (the integer and the fraction). So why would the arguments of pi and &integerPart for modf be needed to find fractionPart, isn’t fractionPart already known?


Strictly speaking they are not known; you have to extract them. Unless you know how the real number (3.14 in your example) is encoded in memory, you will end up writing inefficient code (try it.)

You can extract them using a piece of code you write or by using the library function modf, which will do a much better job.

This is from man modf:


OK, Thank you! So whenever modf is called it always returns the fractional part and copies the integer to an address if supplied?


Yes, now we are cooking :slight_smile:


Thanks Ibex10-next time I’ll read the text more carefully before I call out the Fire Department! :blush:



I bought an electronic version (Kobo) of this book last week and I have reached this chapter today.

On the example, the line of code is written like this :
printf(“integerPart = %.of, fractionPart = %.2f\n”,integerPart, fractionPart);
which gives on the command line : integerPart = 13757774450f, fractionPart = 3.00

shouldn’t it be printf(“integerPart = %.0f, fractionPart = %.2f\n”,integerPart, fractionPart);
and thus return : integerPart = 3, fractionPart = 0.14

could someone explain what the “%.of” is supposed to do ?
thanks in advance

ps, I’m new here, if there is another specific place where I should let this comment / question, please let me know.