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 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
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
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
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 ---
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
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
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
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 **
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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>
|
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...
|
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...
|
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...
| |
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...
|
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;
|
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" ?
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |