• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

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

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1ClaudeFleming  Members   -  Reputation: 104

Like
0Likes
Like

Posted 30 November 2011 - 05:34 PM

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

### #2swiftcoder  Senior Moderators   -  Reputation: 7262

Like
0Likes
Like

Posted 30 November 2011 - 05:53 PM

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;

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?

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.

Tristam MacDonald - SDE II @ Amazon - swiftcoding        [Need to sync your files via the cloud? | Need affordable web hosting?]

### #3ClaudeFleming  Members   -  Reputation: 104

Like
0Likes
Like

Posted 30 November 2011 - 05:58 PM

Thanks. By the way, this is one of the best Web sites in the world.

### #4DenzelM  Members   -  Reputation: 295

Like
0Likes
Like

Posted 30 November 2011 - 05:58 PM

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
Denzel Morris (@drdizzy) :: Software Engineer :: SkyTech Enterprises, Inc.
"When men are most sure and arrogant they are commonly most mistaken, giving views to passion without that proper deliberation which alone can secure them from the grossest absurdities." - David Hume

### #5ClaudeFleming  Members   -  Reputation: 104

Like
0Likes
Like

Posted 30 November 2011 - 06:05 PM

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 :/

### #6ClaudeFleming  Members   -  Reputation: 104

Like
0Likes
Like

Posted 30 November 2011 - 06:08 PM

Halifax 2, I love David Hume!

### #7swiftcoder  Senior Moderators   -  Reputation: 7262

Like
0Likes
Like

Posted 30 November 2011 - 06:15 PM

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.

Tristam MacDonald - SDE II @ Amazon - swiftcoding        [Need to sync your files via the cloud? | Need affordable web hosting?]

### #8SimonForsman  Crossbones+   -  Reputation: 4803

Like
0Likes
Like

Posted 30 November 2011 - 06:17 PM

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.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

### #9TheUnbeliever  Members   -  Reputation: 952

Like
0Likes
Like

Posted 30 November 2011 - 06:24 PM

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.
[TheUnbeliever]

### #10DenzelM  Members   -  Reputation: 295

Like
0Likes
Like

Posted 30 November 2011 - 09:00 PM

Halifax 2, I love David Hume!

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

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.

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 " represent the same thing when assigning to a float."
Denzel Morris (@drdizzy) :: Software Engineer :: SkyTech Enterprises, Inc.
"When men are most sure and arrogant they are commonly most mistaken, giving views to passion without that proper deliberation which alone can secure them from the grossest absurdities." - David Hume

### #11swiftcoder  Senior Moderators   -  Reputation: 7262

Like
0Likes
Like

Posted 30 November 2011 - 09:29 PM

Halifax 2, I love David Hume!

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

There are a lot of us philosophers on this forum

Tristam MacDonald - SDE II @ Amazon - swiftcoding        [Need to sync your files via the cloud? | Need affordable web hosting?]

### #12ClaudeFleming  Members   -  Reputation: 104

Like
0Likes
Like

Posted 02 December 2011 - 10:11 AM

I'm not smart enough to be a philosopher, lol. I just like his description of the beautiful and the sublime.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS