programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

simple calculation - Java vs Calculator

Mikey Greene
Greenhorn
Posts: 7
Hi there in the quest to get my program up and running I have come accross another problem.

I thought I had the calculation right but got a wrong result. I am trying to write a program to calculate the total cost of a journey given mpg, miles and cost of fuel.

The code below shows 3 calculations - the order of values has been shifted around in each instance. When tried in a calculator all results are equal but when run through this program one of the results is different.

Again any help would be appreciated.

Cheers

Mike

Kj Reddy
Ranch Hand
Posts: 1704
Mike,

You are posting in wrong forum. It should go into Intermediate section.

I feel there is some thing wrong wtih your program so you are getting wrong value. Try to understand the operator precedence rules, which I feel you are missing in your code.

For example in the following code:

double Value2=100/40/0.22*0.97;

will be equal to
double Value2=100/40/(0.22*0.97);

double Value1=0.97*100/40/0.22;

will be equal to
double Value1=(0.97*100)/40/0.22;

() will have more precedence, then * , /

So in your code definitely last two values will be different to first value.

I hope you understood.
[ January 18, 2006: Message edited by: KJ Reddy ]

fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
i'm guessing the first one is different???

You have to remember java is dumb. in your first example, it does the operations in order, from left to right. the first thing it sees is "100/40". since you are dividing an int by an int, your answer is an int. that means that you will get 2

you then multiply that by 0.22. since THAT is a float, you now start getting floats - 9.09... and then 8.81.

in your other examples, since the first operation has a float, you get a float back...
0.97*100 returns 97.0 (or thereabouts), 97.0 / 40 = 2.42, and that / .22 is 11.02ish.

Now, having said that, this really is a beginner question, so i'm going to move it to there.

Jeff Albertson
Ranch Hand
Posts: 1780
Originally posted by KJ Reddy:
() will have more precedence, then *, then /

Aren't * and / on the same level of precedence? Isn't 4/2*2 equal to 4, not 1?
[ January 18, 2006: Message edited by: Jeff Albrechtsen ]

Layne Lund
Ranch Hand
Posts: 3061
Originally posted by Jeff Albrechtsen:

Aren't * and / on the same level of precedence? Isn't 4/2*2 equal to 4, not 1?

[ January 18, 2006: Message edited by: Jeff Albrechtsen ]

I agree. And since * and / have equal precedence, they are evaulated from left to right. This means that

is equivalent to

As fred mentioned above, the result of 100/40 is 2 since this uses integer division. Your calculator will probably give 2.5, which is the result of the differences here. If you want to make sure you keep decimal places then you should type "100.0/40.0". This will make the result be a floating point value (with decimals).

HTH

Layne

Kj Reddy
Ranch Hand
Posts: 1704
Originally posted by Jeff Albrechtsen:

Aren't * and / on the same level of precedence? Isn't 4/2*2 equal to 4, not 1?

[ January 18, 2006: Message edited by: Jeff Albrechtsen ]

Jeff,

Thanks for correcting me. I corrected my typo