Java is only calculating to once decimal place

R

rleroux

I'm doing a calculation with a variable defined as a float.
(e.g. take 5% and add 1)
I should get 1.05

When I print the variable that should hold 1.05, I only get 1.0

Needless to say when I use the variable that should have 1.05 stored,
in the rest of my calculation, I'm getting errounious results (i.e.
cannot divide by 0).

I am inputting a figure (integer) from the that needs to be calculated
as a percent. I store the result in a variable as a float


int entry;
float answer;

answer = 1+(entry/100);

thus if I enter 5...

answer = 1+(5/100)
answer = 1.05

when I have java display answer (System.out.println(answer);) I have
1.0 which thows the rest of my calculation that the above formula is
in, out of whack

I understood floats are suppose to have more than one significant digit?
 
G

Gordon Beaton

I'm doing a calculation with a variable defined as a float.
(e.g. take 5% and add 1)
I should get 1.05

When I print the variable that should hold 1.05, I only get 1.0
[...]

I understood floats are suppose to have more than one significant digit?

There is nothing wrong with the float. The problem is that you are
using integer arithmetic to calculate the 5%, which results in 0.

Instead of dividing 5/100, try dividing 5/100.0 or 5/(float)100, etc.

/gordon
 
J

Jussi Piitulainen

I'm doing a calculation with a variable defined as a float.
(e.g. take 5% and add 1)
I should get 1.05 ....
int entry;
float answer;

answer = 1+(entry/100);

No, you are not doing a calculation with a variable defined as a
float, and that is precisely your problem: 5/100 == 0 but 5.0/100
would be quite close to the 0.05 that you expected.

(Your will see that some floating point calculations give results
like 0.049999999999998 instead of the precise 0.05; that's why I
avoid saying simply 5.0/100 == 0.05 above.)
 
G

Googmeister

float, and that is precisely your problem: 5/100 == 0 but 5.0/100
would be quite close to the 0.05 that you expected.

(Your will see that some floating point calculations give results
like 0.049999999999998 instead of the precise 0.05; that's why I
avoid saying simply 5.0/100 == 0.05 above.)

Since 1/20 is not representable as an IEEE floating point number,
the literal 0.05 is not exactly 1/20 either.

In fact (5.0/100 == 0.05) is always true in IEEE, though neither
is exactly 1/20.
 
C

Chris Dollin

I'm doing a calculation with a variable defined as a float.
(e.g. take 5% and add 1)
I should get 1.05

When I print the variable that should hold 1.05, I only get 1.0

It helps a /lot/ if you show /exactly/ what code shows the
problem. Otherwise we have to guess.

Fortuately in this case we have an easy-peasy answer (or two).
int entry;
float answer;

answer = 1+(entry/100);

thus if I enter 5...

answer = 1+(5/100)

5 is an integer literal. 100 is an integer literal. So the / is
an integer divide. So the result is 0. So answer becomes 1(.0).
BOOM.

Try using `answer = 1 + entry/100.0;`.

But note the following:

(a) floats /are not/ decimal. The answer won't be exactly
1.05, but a binary approximation to it.

(b) if you're playing fast-and-loose with floating point
values, you might be better off using doubles, not
floats.

(c) Depending on exactly what you're doing with your
percentages, you might want to consider using decimals
or scaled integers.
 
N

Nigel Wade

I'm doing a calculation with a variable defined as a float.
(e.g. take 5% and add 1)
I should get 1.05

When I print the variable that should hold 1.05, I only get 1.0

Needless to say when I use the variable that should have 1.05 stored,
in the rest of my calculation, I'm getting errounious results (i.e.
cannot divide by 0).

I am inputting a figure (integer) from the that needs to be calculated
as a percent. I store the result in a variable as a float


int entry;
float answer;

answer = 1+(entry/100);

That is a purely integer calculation. 5/100 as an integer is 0.
thus if I enter 5...

answer = 1+(5/100)
answer = 1.05

Arithmetically yes, but computationally the answer is 0, as above.
when I have java display answer (System.out.println(answer);) I have
1.0 which thows the rest of my calculation that the above formula is
in, out of whack

I understood floats are suppose to have more than one significant digit?

Floats do, integers don't have any decimal places. You need to ensure that your
calculation is performed as a floating point operation:

answer = 1.0f+(entry/100.0f);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top