Need help with this float error

G

Glenn

Here is the program:

class floater {
public static void main(String[] arguments) {
float pi = 3.14;
System.out.println("pi=" + pi);
}
}


Here is the compile results.

C:\javasrc\24hours>javac floater.java
floater.java:3: possible loss of precision
found : double
required: float
float pi = 3.14;
^
1 error

Other results lead me to believe that floating point number don't work at all.

e.g

if I store 10/3 in a float type variable the result is 3.0

I installed SDK 1.4.1. Running on Windows ME.

Anyone know what the problem is?

Thanks,

Glenn
 
A

Andrew Rollings

Glenn said:
Here is the program:

class floater {
public static void main(String[] arguments) {
float pi = 3.14;
System.out.println("pi=" + pi);
}
}

try:

float pi = 3.14f;

OR:

float pi = (float)3.14;

whichever you think is clearer.

Andrew
 
R

Ryan Stewart

Glenn said:
Here is the program:

class floater {
public static void main(String[] arguments) {
float pi = 3.14;
System.out.println("pi=" + pi);
}
}


Here is the compile results.

C:\javasrc\24hours>javac floater.java
floater.java:3: possible loss of precision
found : double
required: float
float pi = 3.14;
^
1 error

Other results lead me to believe that floating point number don't work at all.

e.g

if I store 10/3 in a float type variable the result is 3.0

I installed SDK 1.4.1. Running on Windows ME.

Anyone know what the problem is?

Thanks,

Glenn

So you think that the entire Java community is just living with the fact
that Java doesn't support floating point calculations? Java assumes that a
floating point value is a double and that an integer value is an int.
Therefore your first problem is that a double is more precise than a float,
which could cause a loss of precision when changing from the former to the
latter. That's what the compiler is telling you. Try:
float pi = 3.14f;

The 'f' tells it that the preceding number is a float, not a double. As for
your second problem, 10 and 3 are both interpreted as int's. When you
perform the (integer) division 10/3, you get 3 with a remainder of 1. An int
can't use the 1, so it gets tossed. Then you convert the result, 3, to a
float and display it. This can again be solved by appending an 'f' to either
the 10 or the 3 or both. That's not the end of it, either. Try this:

public class Bytes {
public static void main(String[] args) {
byte a = 1;
byte b = a + 1;
}
}
 
A

Andrew Thompson

"Glenn" ...
....
class floater {

Bad name for a class, a class name should
not start with lower case, and this one sounds
like something you'd find at the public pool.
C:\javasrc\24hours>javac floater.java
floater.java:3: possible loss of precision
found : double
required: float
float pi = 3.14;
^

You need a trip here..
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
Then here..
http://groups.google.com.au/groups?group=comp.lang.java.help
 
G

Glenn

This is to all the posters.

Thanks for the info and references.

I kept playing around and found that using double instead of float
"fixed" the problem. So my next question was going to be, "What does
the type "float" actually define?", but most of that is all resolved
now.

The book I am using did not show an example of the "F" appended to a
number.
I don't find it to be intuitive either. If the number has a decimal
point in it, I don't think you should also need to append an "F" to
it. It is not something I would have thought of trying.

And no, I didn't think that Java did not handle floating point
numbers. I did wonder if there was something wrong with my
installation. I looked for answers and for examples of usage, but at
the time I was not finding anything.

At this point, I am assuming that type double will hold a larger FP
number than float will. It doesn't make much sense otherwise to have
a float type. I will check that out later. So you don't have to
comment, but it's fine if you do.

Actually I wouldn't mind an answer to this question at all...

Which book to you like as a reference manual, that lists all the
classes to the core language and defines all the methods?

Thanks,

Glenn
 
G

Glenn

Andrew Thompson said:
"Glenn" ...
...

Bad name for a class, a class name should
not start with lower case, and this one sounds
like something you'd find at the public pool.

But I was Drowning. :)
 
C

Christophe Vanfleteren

Glenn said:
This is to all the posters.

Thanks for the info and references.

I kept playing around and found that using double instead of float
"fixed" the problem. So my next question was going to be, "What does
the type "float" actually define?", but most of that is all resolved
now.

You didn't really fix it, you just used another variable type, that happens
to be more precise, and is the default floating point type in Java, so you
don't have to append F to explicitly tell you want a float.
The book I am using did not show an example of the "F" appended to a
number.
I don't find it to be intuitive either. If the number has a decimal
point in it, I don't think you should also need to append an "F" to
it. It is not something I would have thought of trying.

How would you choose between double and float? They both can have a floating
point in them, and you could define any of them without a floating point at
all, eg. double d = 3;

Many things in a language can't be done by just trying, or by being
intuitive, you just have have to know it. In this case, knowing the
different datatypes and how to use them, is one of the basics of learning a
language. It is a shame if this isn't in your book though.
And no, I didn't think that Java did not handle floating point
numbers. I did wonder if there was something wrong with my
installation. I looked for answers and for examples of usage, but at
the time I was not finding anything.

At this point, I am assuming that type double will hold a larger FP
number than float will. It doesn't make much sense otherwise to have
a float type. I will check that out later. So you don't have to
comment, but it's fine if you do.

Indeed, a float is 32 bits, and a double 64 bits. But you shouldn't just
start using doubles to avoid typing the F, they'll use more memory and or
slower on 32 bits machines.
Actually I wouldn't mind an answer to this question at all...

Which book to you like as a reference manual, that lists all the
classes to the core language and defines all the methods?

Don't use a book for API overviews, use
http://java.sun.com/j2se/1.4.2/docs/api/index.html

You can also download it if you're not always online.

You can also use an IDE that shows the methods and their javadoc inline,
which is very nice to quickly see what you can do with an object.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top