473,513 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on the FAQ

Question 3.15:

Why does the code

double degC, degF;
degC = 5 / 9 * (degF - 32);

keep giving me 0?
Would the following rearrangement solve the problem?

degC = 5 * (degF - 32) / 9;
Topi Linkala
--
"The whole problem with the world is that fools and fanatics are
always so certain of themselves, but wiser people so full of doubts."
- Bertrand Russell
"How come he didn't put 'I think' at the end of it?" - Anonymous
Jun 27 '08 #1
10 1373
On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:
Question 3.15:

Why does the code

double degC, degF;
degC = 5 / 9 * (degF - 32);

keep giving me 0?

Would the following rearrangement solve the problem?

degC = 5 * (degF - 32) / 9;
Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?

--
Robert Gamble
Jun 27 '08 #2
Robert Gamble wrote:
On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:
>>Question 3.15:

Why does the code

double degC, degF;
degC = 5 / 9 * (degF - 32);

keep giving me 0?

Would the following rearrangement solve the problem?

degC = 5 * (degF - 32) / 9;


Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?
Yes I read and it says:

"If both operands of a binary operator are integers, C performs an
integer operation,..."

But in my rewriting there are no binary operation with integer values.
That's why I'm asking if it would work.

Topi
--
"The whole problem with the world is that fools and fanatics are
always so certain of themselves, but wiser people so full of doubts."
- Bertrand Russell
"How come he didn't put 'I think' at the end of it?" - Anonymous
Jun 27 '08 #3
On Apr 13, 3:48 pm, Topi Linkala <n...@iki.fiwrote:
Robert Gamble wrote:
On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:
>Question 3.15:
>Why does the code
>double degC, degF;
degC = 5 / 9 * (degF - 32);
>keep giving me 0?
>Would the following rearrangement solve the problem?
>degC = 5 * (degF - 32) / 9;
Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?

Yes I read and it says:

"If both operands of a binary operator are integers, C performs an
integer operation,..."

But in my rewriting there are no binary operation with integer values.
That's why I'm asking if it would work.
Sorry, I missed the fact that degF was a double. Yes, your example
will work properly since the division will be happening on floating
point values.

--
Robert Gamble
Jun 27 '08 #4
Topi Linkala wrote:
Robert Gamble wrote:
>On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:
>>Question 3.15:

Why does the code

double degC, degF;
degC = 5 / 9 * (degF - 32);

keep giving me 0?

Would the following rearrangement solve the problem?

degC = 5 * (degF - 32) / 9;


Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?

Yes I read and it says:

"If both operands of a binary operator are integers, C performs an
integer operation,..."

But in my rewriting there are no binary operation with integer values.
That's why I'm asking if it would work.

Topi
The statement..

degC = 5 / 9 * (degF - 32);

...can't work because 5 / 9 is (int) zero. Also degF is not initialized.

degC = (degF - 32) * 5 / 9;

should work better for you.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 27 '08 #5
Joe Wright wrote:
Topi Linkala wrote:
>Robert Gamble wrote:
>>On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:

Question 3.15:

Why does the code

double degC, degF;
degC = 5 / 9 * (degF - 32);

keep giving me 0?

Would the following rearrangement solve the problem?

degC = 5 * (degF - 32) / 9;

Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?


Yes I read and it says:

"If both operands of a binary operator are integers, C performs an
integer operation,..."

But in my rewriting there are no binary operation with integer values.
That's why I'm asking if it would work.

Topi


The statement..

degC = 5 / 9 * (degF - 32);

..can't work because 5 / 9 is (int) zero. Also degF is not initialized.

degC = (degF - 32) * 5 / 9;

should work better for you.
I don't think so because there's no reason to believe that the
multiplication is done before the division, but in my example that
doesn't matter as one of the operands is double in any case.

Topi
--
"The whole problem with the world is that fools and fanatics are
always so certain of themselves, but wiser people so full of doubts."
- Bertrand Russell
"How come he didn't put 'I think' at the end of it?" - Anonymous
Jun 27 '08 #6
On Apr 13, 4:53 pm, Topi Linkala <n...@iki.fiwrote:
Joe Wright wrote:
Topi Linkala wrote:
Robert Gamble wrote:
>On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:
>>Question 3.15:
>>Why does the code
>>double degC, degF;
degC = 5 / 9 * (degF - 32);
>>keep giving me 0?
>>Would the following rearrangement solve the problem?
>>degC = 5 * (degF - 32) / 9;
>Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?
Yes I read and it says:
"If both operands of a binary operator are integers, C performs an
integer operation,..."
But in my rewriting there are no binary operation with integer values.
That's why I'm asking if it would work.
Topi
The statement..
degC = 5 / 9 * (degF - 32);
..can't work because 5 / 9 is (int) zero. Also degF is not initialized.
degC = (degF - 32) * 5 / 9;
should work better for you.

I don't think so because there's no reason to believe that the
multiplication is done before the division, but in my example that
doesn't matter as one of the operands is double in any case.
Actually, the multiplication is guaranteed to be performed before the
division because they both have the same precedence and are left-to-
right associative.

--
Robert Gamble
Jun 27 '08 #7
Topi Linkala wrote, On 13/04/08 20:48:
Robert Gamble wrote:
>On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:
>>Question 3.15:

Why does the code

double degC, degF;
degC = 5 / 9 * (degF - 32);

keep giving me 0?

Would the following rearrangement solve the problem?

degC = 5 * (degF - 32) / 9;

Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?

Yes I read and it says:

"If both operands of a binary operator are integers, C performs an
integer operation,..."
Which is why it does not work.
But in my rewriting there are no binary operation with integer values.
Which is why it would work.
That's why I'm asking if it would work.
You obviously did not make it clear enough to Robert that you had read
it. Don't worry about it.
--
Flash Gordon
Jun 27 '08 #8
Topi Linkala wrote:
>
Question 3.15:

Why does the code
double degC, degF;
degC = 5 / 9 * (degF - 32);
keep giving me 0?
Think about it. 5 and 9 are ints. What is the value of 5/9?. How
many times can you subtract 9 from 5 and have the result
non-negative? What is the result of that multiplied by (degF -32)?
>
Would the following rearrangement solve the problem?
degC = 5 * (degF - 32) / 9;
That depends on the type of degF. For double, yes. Much simpler
to use 5/9.0.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #9
CBFalconer wrote:
Topi Linkala wrote:
>Question 3.15:

Why does the code
double degC, degF;
degC = 5 / 9 * (degF - 32);
keep giving me 0?

Think about it. 5 and 9 are ints. What is the value of 5/9?. How
many times can you subtract 9 from 5 and have the result
non-negative? What is the result of that multiplied by (degF -32)?
>Would the following rearrangement solve the problem?
degC = 5 * (degF - 32) / 9;

That depends on the type of degF.
If you read the OP, degF was declared as double.

--
Ian Collins.
Jun 27 '08 #10
Topi Linkala wrote:
...
But what stops the compiler to calculate the 5/9 first as it is a
constant expression and can be done on compile time?
Nothing really. In the expresison 'E * 5/9' the compiler is free to
calculate '5/9' in advance. What the compiler _cannot_ do is to destroy
the semantics if the full expression (derived from the grammar) and
evaluate '5/9' as an integral division. If the compiler want to
pre-evaluate '5/9', it can do that, but it has to make sure that the
result is precise enough so that the whole thing works "as if" it
evaluated 'E * 5' first and then divided the result by '9'.
AFAIK only guaranteed sequencing on expressions are:

1. precedence
Precedence does not guarantee any sequencing. If defines the semantics
of the expression, i.e. it disambiguates the result. It tells you that
'2 + 2 * 2' is 6 and not 8. But the compiler is perfectly free to forget
about these 2's and obtain that final 6 as '37 / 5 + 1' if it is so
inclined.
2. comma operation
3. boolean operations || and &&
4. ?:
These three do indeed introduce real sequencing.
So an expression a*b/c can be calculated (a*b)/c or a*(b/c).
Yes, it can be calculated as 'a*(b/c)' as long as the result is the same
as '(a*b)/c'. If on the given platform 'a*(b/c)' give a different
result, then it the compiler will have no choice but calculate it as
'(a*b)/c'.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3083
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
4997
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
2629
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
3059
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
3390
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
3682
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
4021
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
4696
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
4252
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
2539
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.