Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Java is only calculating to once decimal place

Thread view: 
rleroux@telus.net - 26 Jan 2007 09:52 GMT
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?
Gordon Beaton - 26 Jan 2007 10:12 GMT
> 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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Jussi Piitulainen - 26 Jan 2007 10:19 GMT
> 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.)
Googmeister - 26 Jan 2007 10:28 GMT
On Jan 26, 5:19 am, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
wrote:
> rler...@telus.net writes:
> > I'm doing a calculation with a variable defined as a float.
[quoted text clipped - 7 lines]
> 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.
Chris Dollin - 26 Jan 2007 10:32 GMT
> 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;
[quoted text clipped - 4 lines]
>
> 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.

Signature

Chris "electric hedgehog" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Nigel Wade - 26 Jan 2007 10:50 GMT
> I'm doing a calculation with a variable defined as a float.
> (e.g. take 5% and add 1)
[quoted text clipped - 13 lines]
>
> 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);

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.