Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I'm stumped by math in Java

1 view
Skip to first unread message

Heather B

unread,
Sep 20, 2003, 1:10:00 PM9/20/03
to

I'm stumped. How do I get this to return a string that contains a decimal
value derived by dividing two integers? This is what I have and it returns
"3.0" but I need "3.333".

public class Test {
public String getAverage() {
int iSumOfGrades = 10;
int iNumberOfGrades = 3;
float dAverageGrade = 0;
dAverageGrade = iSumOfGrades/iNumberOfGrades;
return (new Float(dAverageGrade)).toString();
}

public static void main(String[] args) {
Test t = new Test();
System.out.println(t.getAverage());
}
}


Marco Schmidt

unread,
Sep 20, 2003, 1:27:28 PM9/20/03
to
fup2 comp.lang.java.programmer

Heather B:

>I'm stumped. How do I get this to return a string that contains a decimal
>value derived by dividing two integers? This is what I have and it returns
>"3.0" but I need "3.333".
>
>public class Test {
> public String getAverage() {
> int iSumOfGrades = 10;
> int iNumberOfGrades = 3;
> float dAverageGrade = 0;
> dAverageGrade = iSumOfGrades/iNumberOfGrades;

Here you're doing integer division. 10 / 3 is 3 if all you have are
integers. If you want this to be a float division, explicitly cast the
values to that type:

dAverageGrade = (float)iSumOfGrades/(float)iNumberOfGrades;

> return (new Float(dAverageGrade)).toString();

A simple

return Float.toString(dAverageGrade);

is enough here.

Check out the package java.text for more advanced formatting options.

Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html

Todd Shillam

unread,
Sep 20, 2003, 1:31:19 PM9/20/03
to
It's because your using two integer variables to perform the calculations;
hence, when you divide 10 by 3, the results are 3.0--the decimal positions
get dropped. You need to change your variables to float or double data
types.

Best regards,

Todd

"Heather B" <heat...@glinberg.com> wrote in message
news:vmp2g0q...@corp.supernews.com...

Roedy Green

unread,
Sep 20, 2003, 2:12:13 PM9/20/03
to
On Sat, 20 Sep 2003 12:10:00 -0500, "Heather B"
<heat...@glinberg.com> wrote or quoted :

>I'm stumped. How do I get this to return a string that contains a decimal
>value derived by dividing two integers? This is what I have and it returns
>"3.0" but I need "3.333".

See http://mindprod.com/jgloss/floatingpoint.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Tr0mBoNe-

unread,
Sep 20, 2003, 6:16:20 PM9/20/03
to
Basically in my programming style, if i want a floating point result,
i do all my math in floating point numbers. casting them or just
making the result of the type you want just leads to one of my
nightmares, off by one errors.

work in one, send in one... that just make more sence to me

andrew

I still wont mention the total paradox produced by the title "math in
java"

"Heather B" <heat...@glinberg.com> wrote in message news:<vmp2g0q...@corp.supernews.com>...

thedogfarted

unread,
Sep 26, 2003, 1:00:22 PM9/26/03
to
<code>dAverageGrade = (float) iSumOfGrades/iNumberOfGrades;</code>
instead of
<code>dAverageGrade = iSumOfGrades/iNumberOfGrades;</code>


thedogfarted *

Phil...

unread,
Sep 26, 2003, 7:33:13 PM9/26/03
to
1. since double is just as fast or faster than float, use double.
2. are you sure the integer will be promoted to float in your example?
should you not use
dAverageGrade = (double) iSumOfGrades/(double)iNumberOfGrades;
or is this redundant?


"thedogfarted" <thedog...@inbox.lv> wrote in message
news:bl1rb7$ten$1...@milzis.latnet.lv...

Roedy Green

unread,
Sep 26, 2003, 8:37:51 PM9/26/03
to
On Fri, 26 Sep 2003 23:33:13 GMT, "Phil..." <ry...@ieee.org> wrote or
quoted :

>2. are you sure the integer will be promoted to float in your example?
>should you not use
>dAverageGrade = (double) iSumOfGrades/(double)iNumberOfGrades;
>or is this redundant?

you only need one (double). Division automatically promotes the other
operand to match. Code generated is identical.

Mark Thornton

unread,
Sep 27, 2003, 4:22:41 AM9/27/03
to
Phil... wrote:

> 1. since double is just as fast or faster than float, use double.

Whether this is true or not depends on your hardware. I believe that on
some of the RISC type cpus float is significantly faster than double.
However the use of float should be restricted to those who have a good
understanding of floating point types.

Mark Thornton

Phil...

unread,
Sep 27, 2003, 11:12:21 AM9/27/03
to
Disagree do I.
I ran a test and double was faster at multiply than fixed by a tiny bit
(did not try other operations) despite the fact that my
Celeron CPU has support for float but not double.
Recall that 1.4.2 for MS is still interpreted.

Phil...


"Mark Thornton" <mark.p....@ntl-spam-world.com> wrote in message
news:lNbdb.913$QH3...@newsfep4-winn.server.ntli.net...

Phil...

unread,
Sep 27, 2003, 11:23:17 AM9/27/03
to
Sorry, I mean double was faster than float.

Date d0 = new Date();
long t0 = d0.getTime();
float f1 = 0.0f, f2 = 0.0f;
double f3 = 0.0;
for(long i=0; i<100000000; i++) {
f2 = (float) i * 17.0f;
}
Date d1 = new Date();
long t1 = d1.getTime();
for(long i=0; i<100000000; i++) {
f3 = (double) i * 17.0;
}

"Phil..." <ry...@ieee.org> wrote in message
news:pNhdb.163868$mp.8...@rwcrnsc51.ops.asp.att.net...

Mark Thornton

unread,
Sep 27, 2003, 11:47:20 AM9/27/03
to
Phil... wrote:

> Disagree do I.
> I ran a test and double was faster at multiply than fixed by a tiny bit
> (did not try other operations) despite the fact that my
> Celeron CPU has support for float but not double.
> Recall that 1.4.2 for MS is still interpreted.
>
> Phil...

The celeron is not one of the CPU that I was considering. Any method run
more than a few times in 1.4.2 (for Windows) will be compiled (unless
you have disabled the JIT).
The 1.4.2 server JVM will use SSE and SSE2 instructions where these are
available. SSE will improve float performance while SSE2 does the same
for double. In the absence of either of these (or when using client
compiler) the old FPU instructions are used. In this case the time taken
for float is very similar to that for double (and can be longer
depending on the costs of switching the accuracy mode).

Now try comparing float vs double performance on SPARC or Power processors.

Mark Thornton

Roedy Green

unread,
Sep 27, 2003, 2:24:32 PM9/27/03
to
On Sat, 27 Sep 2003 15:12:21 GMT, "Phil..." <ry...@ieee.org> wrote or
quoted :

>Disagree do I.


>I ran a test and double was faster at multiply than fixed by a tiny bit
>(did not try other operations) despite the fact that my
>Celeron CPU has support for float but not double.
>Recall that 1.4.2 for MS is still interpreted.

You don't disagree. The Celeron is not a RISC chip. In the Celeron,
the floating point works as a 64-bit double plus a few guard bits.
That is its native mode.

Phil...

unread,
Sep 28, 2003, 1:23:19 AM9/28/03
to
If you go to the Intel site and look at the "IA-32 Intel Architecture
Software Developer's Manual - Volume 1: Basic Architecture"
you will find that they use IEEE Standard 754 Floating-point
formats and the SSE2 instructions are needed for
64-bit double floating point operations. Further
you will see that SSE2 instructions were introduced into
the IA-32 architecture with the Pentium 4 and Intel Xeon processors.
Since the Celeron is an Intel processor that
was available prior to the Pentium 4 and Xeon
it does not have what is called "hardware floating point" for
double precision, only single precision.
The word RISC and the string "reduced instruction" do not
exist in the manual.
Ok, so I redid my test with division instead of multiplication
and float is maybe 10 or 15 percent faster than double.

Now I want to fully understand you, are you saying that the
Celeron has only one kind of floating point and that it is 64 bit
but that it is considered Float rather than Double? Or that at
least the J2SE 1.4.2 jit uses is as a Float?

HTH, Phil... (I don't sound defensive do I?)

"Roedy Green" <ro...@seewebsite.com> wrote in message
news:2clbnv8tufnm67qb7...@4ax.com...

0 new messages