# Simple questions about mathematical operations in C and C++

This topic is 2879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am reading an Amazon Kindle book on C programming and the author of it states that if you are dividing two integers together, such as 550 and 24, and you want floating-point division to occur, then you must either explicitly cast one or both of the integers to floats, or if one of the integers is a constant, say a number like 24, then you can either type it like this-- 24.0-- or explicitly cast it to a float (I think?). Adding a decimal point to it (does it also need a number after the decimal point to be considered a float for the time being?) converts it to a float, and floating-point division occurs.

I understand that and love programming because of all the little rules (it's as cool as Dungeons and Dragons, hehe), but I have a question.

What if you have four variables, as follows:
int a = 1;
int b = 2;
int c = 3;
float d = 4.0;

Now, with those variables I am wondering, does the C compiler consider the following floating-point division for all variables and not truncate the intermediate results?
float result = a/b + c*d;

Another question I have is this: are all arithmetic operations broken down into binary operations? Would a*b*c+d - b+4 be turned into a binary tree data structure or if that's not possible, in general, how are all the variables and constants stored and operated on by the compiler?

I would really appreciate it if even one person answered these questions.

Thanks a lot,

-Lee

##### Share on other sites

Adding a decimal point to it (does it also need a number after the decimal point to be considered a float for the time being?) converts it to a float, and floating-point division occurs.

Not quite. Adding a decimal point (with or without a number after it) makes it a double. You have to append the character 'f' to make it a float:
2.0 // double 2.0f // float
However, assignment causes automatic type conversion to whatever type variable you are assigning to (for legal conversions).
What if you have four variables, as follows:
int a = 1; int b = 2; int c = 3; float d = 4.0;

Now, with those variables I am wondering, does the C compiler consider the following floating-point division for all variables and not truncate the intermediate results?
float result = a/b + c*d;[/quote]
You are assigning the result to a float, so there is no reason for truncation. If you assign to an integer (as I assume you meant to do), then yes, the truncation will happen after the calculations.

Another question I have is this: are all arithmetic operations broken down into binary operations? Would a*b*c+d - b+4 be turned into a binary tree data structure or if that's not possible, in general, how are all the variables and constants stored and operated on by the compiler?[/quote]
It will be turned into a sequence of single operations, which can be executed in order, yes. I'm not quite sure what you are asking with the 'binary tree' bit.

##### Share on other sites
Thanks. By the way, this is one of the best Web sites in the world.

##### Share on other sites

does it also need a number after the decimal point to be considered a float for the time being?

It really depends on the compiler. Some compilers need a number between 0 and 9 after the decimal point, other compilers may accept the letter 'f', and still other compilers may just require the decimal point. Some common ways to represent the same thing when assigning to a float: 4., 4.f, and 4.0.

Now, with those variables I am wondering, does the C compiler consider the following floating-point division for all variables and not truncate the intermediate results?
float result = a/b + c*d;

No, it does not. The compiler evaluates that expression using an order of operations. Therefore a/b we be evaluated first as an integer divided by an integer; returning an integer. In this case it will return 0 because integers truncate. Next c*d will be evaluated resulting in a float multiplied by a float, which returns a float (12.0). Next it evaluates the addition between an integer (0) and a float (12.0), resulting in a float (12.0) which is then stored as your result. This is different from the result of 12.5 that you were probably expecting.

Another question I have is this: are all arithmetic operations broken down into binary operations? Would a*b*c+d - b+4 be turned into a binary tree data structure or if that's not possible, in general, how are all the variables and constants stored and operated on by the compiler?

Short answer: yes. Long answer: not quite. Compilers use a myriad of techniques to digest source code. There are multiple stages and representations that your program goes through before an executable is produced. As an example GCC uses internal representations called GENERIC, GIMPLE, and RTL among other things.

EDIT: Ninja'd

##### Share on other sites
I have another question, but if you are busy, please don't mind me, lol. In my above post, if the variables 'a' and 'b' are ints and dividing them would in reality yield a floating-point result, but would they be divided using integer division in a problem like the following? Ex: a/b + 104/24.0? In a 'complicated' mathematical problem that involves many different types of numbers, are all of the numbers promoted to the highest-precision number that is in the math problem in question? Sorry if this is such a silly question :/

##### Share on other sites
Halifax 2, I love David Hume!

##### Share on other sites

It really depends on the compiler. Some compilers need a number between 0 and 9 after the decimal point, other compilers may accept the letter 'f', and still other compilers may just require the decimal point. Some common ways to represent the same thing when assigning to a float: 4., 4.f, and 4.0.

It is not up to the compiler - the standard specifies all those as valid floating point literals, as well as numerous other variations (all of which can be followed by an optional precision specifier of f, d or l):
4.2 4. .2 4.2e-5 4e-5 .2e+2 
(also note that two of your examples are doubles, the second one only is a float)

IBM's compiler documentation goes into more detail.

##### Share on other sites

I am reading an Amazon Kindle book on C programming and the author of it states that if you are dividing two integers together, such as 550 and 24, and you want floating-point division to occur, then you must either explicitly cast one or both of the integers to floats, or if one of the integers is a constant, say a number like 24, then you must type it like this: 24.0. Adding a decimal point to it (does it also need a number after the decimal point to be considered a float for the time being?) converts it to a float, and floating-point division occurs.

I understand that and love programming because of all the little rules (it's as cool as Dungeons and Dragons, hehe), but I have a question.

What if you have four variables, as follows:
int a = 1;
int b = 2;
int c = 3;
float d = 4.0;

Now, with those variables I am wondering, does the C compiler consider the following floating-point division for all variables and not truncate the intermediate results?
float result = a/b + c*d;

Another question I have is this: are all arithmetic operations broken down into binary operations? Would a*b*c+d - b+4 be turned into a binary tree data structure or if that's not possible, in general, how are all the variables and constants stored and operated on by the compiler?

I would really appreciate it if even one person answered these questions.

Thanks a lot,

-Lee

float result = a/b + c*d will be evaluated as:

temp = a/b (This becomes an int as both a and b are ints and is truncated to 0)
temp2 = c*d (This becomes a float as one operand is a float, thus: 12.0f)
result = temp + temp2 (This becomes a float as one operand is a float , 12.0f)

the assignment doesn't cause any additional conversions (But if for example D had been 4.1 instead and result had been of the int type then it would be truncated again to 12.0 rather than 12.3)

See:
http://msdn.microsof...v=vs.80%29.aspx (It shows you that multiplication and division has the same precedence and thus is evaluated in the order given , addition is lower and will be evaluated late, assignment is done at the very end)

I have another question, but if you are busy, please don't mind me, lol. In my above post, if the variables 'a' and 'b' are ints and dividing them would in reality yield a floating-point result, but would they be divided using integer division in a problem like the following? Ex: a/b + 104/24.0? In a 'complicated' mathematical problem that involves many different types of numbers, are all of the numbers promoted to the highest-precision number that is in the math problem in question? Sorry if this is such a silly question :/

It will be truncated, each operation is evaluated (and converted/truncated if needed) separatly in the order specified in the link above.

a/b + 104/24.0 will evaluate a/b first , if both a and b are ints it will use integer division and store the result temporarily

then it will evaluate 104/24.0 using floating point division (Converting 104 to a float which may or may not be exactly 104 (Some integer numbers are not available as floats)

and finally it will convert the result of a/b to the nearest available floating point number and perform a floating point division using the two temporary results.

##### Share on other sites
In a 'complicated' mathematical problem that involves many different types of numbers, are all of the numbers promoted to the highest-precision number that is in the math problem in question?

No, it works exactly the way your book describes it. If you divide an integer by an integer, that is integer division and you'll get truncation, regardless of whether you then go on to add a float or not.

##### Share on other sites

Halifax 2, I love David Hume!

I'm surprised to find someone else who knows of him.

[quote name='Halifax2' timestamp='1322697529' post='4889237']
It really depends on the compiler. Some compilers need a number between 0 and 9 after the decimal point, other compilers may accept the letter 'f', and still other compilers may just require the decimal point. Some common ways to represent the same thing when assigning to a float: 4., 4.f, and 4.0.

It is not up to the compiler - the standard specifies all those as valid floating point literals, as well as numerous other variations (all of which can be followed by an optional precision specifier of f, d or l):
4.2 4. .2 4.2e-5 4e-5 .2e+2 
(also note that two of your examples are doubles, the second one only is a float)

IBM's compiler documentation goes into more detail.
[/quote]
You are correct, I used the wrong words. Furthermore, I realize what you are saying in your note and I considered rewriting my sentence in order to make it clearer. In the end I just chose to say "[color=#1C2837][size=2] represent the same thing when assigning to a float."

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 21
• 11
• 25