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


#1

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?


#2

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:


#3

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


#4

Yes, now we are cooking :slight_smile:


#5

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


#6

Hello,

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.

Olivier