Need help with this float error

Discussion in 'Java' started by Glenn, Feb 1, 2004.

  1. Glenn

    Glenn Guest

    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
     
    Glenn, Feb 1, 2004
    #1
    1. Advertising

  2. "Glenn" <> wrote in message
    news:...
    > 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
     
    Andrew Rollings, Feb 1, 2004
    #2
    1. Advertising

  3. Glenn

    Ryan Stewart Guest

    "Glenn" <> wrote in message
    news:...
    > 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;
    }
    }
     
    Ryan Stewart, Feb 1, 2004
    #3
  4. "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

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Feb 1, 2004
    #4
  5. Glenn

    Glenn Guest

    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
     
    Glenn, Feb 2, 2004
    #5
  6. Glenn

    Glenn Guest

    "Andrew Thompson" <> wrote in message news:<Y4_Sb.37918$>...
    > "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.
    >


    But I was Drowning. :)
     
    Glenn, Feb 2, 2004
    #6
  7. Glenn wrote:

    > 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.

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Feb 2, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,334
    Roedy Green
    May 10, 2004
  2. Jim West
    Replies:
    4
    Views:
    1,517
    Jeff Schwab
    Jan 16, 2004
  3. Jukka Lehtonen

    operator== (float, float)

    Jukka Lehtonen, Aug 4, 2004, in forum: C++
    Replies:
    5
    Views:
    2,458
    Jukka Lehtonen
    Aug 5, 2004
  4. bd
    Replies:
    0
    Views:
    673
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,660
    James Kanze
    Oct 8, 2009
Loading...

Share This Page