![]() |
|
#1
|
|||
|
|||
Simple Question Regarding PrecedenceAnother newbie question from me.
I'm doing a really basic exercise to practice precedence rules in Java. The rules seem pretty straight forward to me, but something that I feel like should work simply doesn't. The exercise is to write a console program that converts Farenheit temperature to Celsius, based on the following equation: Quote:
The class I wrote works perfectly and I understand how it conforms to precedence rules in Java: JAVA Code:
Here's what I DON'T understand. Why does the following expression return a "0.0" regardless of what initial value I enter for Farenheit? I feel like it still conforms to Java's precedence rules, but obviously I'm still not getting something (and perhaps it has nothing to do with precendence). JAVA Code:
I thought that the computations within the parenthesis were done first. Sorry for the super basic questions! I'm doing this solo so I appreciate folks' taking the time out. |
|||
|
#2
|
|||
|
|||
Re: Simple Question Regarding PrecedenceWhat you are not understanding is that when the compiler sees that statement, it assigns 5 and 9 to an integer. So, when you compute the integer division of 5/9, you get an integer back which is zero. Make it 5.0 and see what happens.
|
|
#3
|
|||
|
|||
Re: Simple Question Regarding PrecedenceAh that makes sense. So always express a number as x.0 if I want the compiler to treat it as a double.
Are there other ways to tell it to treat all values in an equation as doubles? Thanks for setting me straight on that one! |
|
#4
|
|||
|
|||
Re: Simple Question Regarding PrecedenceQuote:
JAVA Code:
Consistent with my personal style (and noting that you misspelled "Fahrenheit"), I probably would have written it JAVA Code:
Cleaner appearing and not as many of those annoying parentheses to keep track of. Bottom line: If the division has only integers, the quotient will be the result of integer division. If one operand of a binary arithmetic operation is a double and the other is an integer, the integer is "promoted" and the operation will be done with floating point arithmetic. Regards, Dave |
|
#5
|
|||
|
|||
Re: Simple Question Regarding PrecedenceThat clears up a lot (down to the spelling).
So basically, because "5 / 9" is enclosed in parentheses, that computation is done first. And since both operands are integers, I end up with the integer 0. And when I rewrite the expression as "5 * (F-32) / 9," the 5 is converted into 5.0 because (F-32) is treated as a double, and the 9 becomes 9.0 for the same reason. So that works, but of course doesn't look as nice. "(5.0 / 9.0) * (fahrenheit - 32)" definitely wins on all fronts. You've cleared up a lot for me. Thanks for taking the time out on the remedial end of things! I'm sure I'll be back in another chapter or two. |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple array question | steve_d | C++ Forum | 2 | 02-May-2007 03:40 |
| Problems about simple tricked loop question | allenfanwenyuan | C++ Forum | 6 | 01-May-2007 09:19 |
| simple question | betazee | C Programming Language | 5 | 07-Jun-2006 13:08 |
| Simple question about while loop | Lej | C++ Forum | 6 | 07-Aug-2005 13:57 |
| Simple question on arrays--please help! | brookeville | C++ Forum | 16 | 17-Nov-2004 23:23 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The