# "You give modf() a double"?

#1

Hey guys,

I’m a new to programming, and I’m starting to learn with this book (I hope that’s a good idea). I’ve been getting through everything pretty well thus far. But one thing that I don’t think I have a good grasp of is giving a function a double, such as giving modf() a double. Dose giving the function a double just allow it to have hold a variable that has a bigger decimal? I removed “double” from pi and fractionPart, and I still got the correct output. Why is that? I apologize in advance if I’m way off, and any help would be appreciated. Thank you in advance.

#2

Roughly speaking, there are two kinds of numbers: integer numbers and real numbers.

Real numbers can have fractional parts, whereas integers can’t.

For example, it is not possible to represent the rational number 22/7 as an integer number, but the same number can be represented as a real number (3.142857).

When a function takes a double (a type for representing real numbers) as an argument, you can give it an integer number or a real number; it will receive the number you give without any loss (roughly speaking again).

However, if you give a real number to a function taking an integer as an argument, it will receive only the whole part (integer part) of the number; the fractional part is lost.

Here is an example:

``````//  main.m

#import <Foundation/Foundation.h>

#define MY_Log1(P1, P2) NSLog (@"%s: "P1, __PRETTY_FUNCTION__, P2)

void PrintLong (long);
void PrintDouble (double);

int main (int argc, const char * argv[])
{
const double MY_PI = 22.0 / 7.0;

PrintLong (MY_PI);
PrintDouble (MY_PI);

return 0;
}

void PrintLong (long v)
{
MY_Log1("v=%ld", v);
}

void PrintDouble (double v)
{
MY_Log1("v=%f", v);
}``````

Its output:

``````2012-08-17 22:31:22.692 RealNumbers[15843:403] void PrintLong(long): v=3
2012-08-17 22:31:22.694 RealNumbers[15843:403] void PrintDouble(double): v=3.142857``````

#3

Thank you for the reply. That makes complete sense. But, since it can return either a whole number or a real number, why don’t we just always use double then? Does it have to do with preserving RAM?

#4

The precision that you require of values you are working with is the primary factor; ram is only secondary.

For example if you are counting sheep, the natural number type to use is an integer type such as char, short, int, long, long long; or their unsigned counter parts. However if you are weighing gold, the most natural number type to use is a real type such as float, double, etc.

In today’s programming languages, the largest number you can store in a real is much greater than that you can store in an integer; and real numbers are harder to represent and operate on than integers.

#5

ibex10 you are a a serious guy! How long have you been studying programming? Your answers are always so precise and even though they drive me nuts to start with I keep coming back til I get what your saying BUT I need to ask: Why are your code examples written using all kinds of terms we have not covered yet? You use what seems like ‘OBJ-C’ code while we are still in the ‘C’ chapters? Makes it difficult to understand what your explaining sometimes…
But either way man, Thank You so much for your contributions because they take this forum to another level!
ALSO, I would like to know where I can find the ‘[color=#FF0000]code[/color]’ for library functions like ‘modf()’ please if anyone can point me to it… Ive tried looking on the net but only the explanations as to its purpose are given and not the actual code of the function.
Thank You!