Results 1 to 13 of 13
  1. #1
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default getting weird answer when multipling

    when im multilpying using this code

    a*.1

    im keep get this answer

    0.a000000000000001

    i dont understand where its coming from, all i want is .a without all the other stuff, what is the easiest way of doing this and why is it happening?

    a is an int, and im using the newest versoin of netbeans

    any help is greatly apperciated

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: getting weird answer when multipling

    Please post the full text of the statement(s) you are using that creates that output.

  3. #3
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: getting weird answer when multipling

    int tempDays = 30 - days[i];

    else if (tempDays < 0){
    String lateFee = Double.toString((tempDays*-1)*.1);
    JLabel label7 = new JLabel ("$ " + lateFee);

    this enough? its for a gui window

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: getting weird answer when multipling

    Your posted code does not have a print statement. Where do you see the String that you posted?
    0.a000000000000001
    Show the code that builds that String. Add a debug println() statement to print it out so we can see what yor code is doing.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: getting weird answer when multipling

    It's just the nature of how multiplication works on computer. Try using BigDecimal (I think that it). http://docs.oracle.com/javase/6/docs...malFormat.html
    Last edited by sunde887; 12-10-2011 at 08:18 PM.

  6. #6
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: getting weird answer when multipling

    sorry for being confusing, this is the window that is printed
    getting weird answer when multipling-help-window.gif

    and i want it to display .7 instead of .700000000000001

    this is the whole for loop that figures out how much each book is over due by if at all
    for (int i=0; i <= (books.length-1); i++){
    int tempDays = 30 - days[i];
    if (tempDays >= 0){
    String outDays = Integer.toString(tempDays);
    JLabel label6 = new JLabel (outDays);
    box1.add(label6);
    box1.add(Box.createVerticalStrut(5));
    JLabel label7 = new JLabel ("N/A");
    box2.add(label7);
    box2.add(Box.createVerticalStrut(5));
    }
    else if (tempDays < 0){
    String outDays = Integer.toString(tempDays*-1);
    JLabel label6 = new JLabel ("Over due by " + outDays + " day(s)");
    box1.add(label6);
    box1.add(Box.createVerticalStrut(5));
    String lateFee = Double.toString((tempDays*-1)*.1);
    JLabel label7 = new JLabel ("$ " + lateFee);
    box2.add(label7);
    box2.add(Box.createVerticalStrut(5));
    }
    }
    would the easiest thing to do be subtract the 0.0000000000000001 or is there a ronding class?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: getting weird answer when multipling

    See post # 5

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: getting weird answer when multipling

    There are number format classes that will allow you to specify the number of decimal places.
    Also the String class has a format method.

  9. #9
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: getting weird answer when multipling

    sweet it works now thanks for the help

  10. #10
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: getting weird answer when multipling

    That "1" at the end doesn't make any sense as repeating decimals would make all the digits after 10 digits the same as the first 10, which is "0".

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: getting weird answer when multipling

    That "1" at the end doesn't make any sense
    On the contrary the Java Language Specification in section 4.2.3 Floating-Point Types, Formats, and Values precisely gives the sense: "The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York)."

    Not that I've ever read IEEE 754-1985, but the general principle is clear enough. The language only deals with floating point quantities using a finite amount of precision: there are, after all, infinitely many such quantities so that representing them all uniquely would require an infinite amount of memory/paper/screen space/time etc. The point is that, there being an infinite number of them, the symbols or bit patterns in memory for at least some of them would have to be arbitrarily large. So the language (and the computer) is obliged to work with imprecise surrogates which are "near enough".

    (Integer values suffer from the same problem, but here the computer offers complete precision but at a cost of ignoring all the integer values past a certain point. Even with limited precision the floating point values must also be limited. The bottom line is you can only specify a finite number of different numbers.)

    Floating point numbers are, in a sense, "fuzzy". You get a certain number of decimal places, and that's it. Where an actual real world quantity is being represented this way there are no grounds for thinking that the digits you don't see are zero, or seven, or anything else. They are simply not represented.

    Although the relationship between Java floating point types (and those other languages) and real world quantities is that of "imprecise resemblance", the operations on them do have to be defined precisely (otherwise nobody would know what is going on). That is where the IEEE standard comes in and, as far as I know, Java adheres to it exactly.

    Of course the IEEE folk may have cooked up a crazy standard! Their cleverness and the usefulness of the standard over time is only so much evidence in their favour. But before suggesting a better or more straight forward alternative it might pay to read the standard and consider the issues that it addresses.
    Last edited by pbrockway2; 12-10-2011 at 11:34 PM.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: getting weird answer when multipling

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: getting weird answer when multipling

    The representation of 0.1

    is not a recurring fraction in base 10.

    It is a recurring fraction in binary -- namely 0-000110011'0011'

    - so you have rounding error, because the CPU works in binary, not in the decimal base in which you have written the number.

Similar Threads

  1. i want to float the answer
    By vibaviattigala in forum New To Java
    Replies: 2
    Last Post: 07-16-2011, 05:02 PM
  2. Compare true Answer /Player answer
    By Eilime in forum New To Java
    Replies: 1
    Last Post: 04-24-2011, 09:51 PM
  3. Could anyway answer me this?
    By Jojomofoman in forum New To Java
    Replies: 14
    Last Post: 12-17-2010, 12:14 AM
  4. Replies: 3
    Last Post: 11-02-2008, 02:29 AM
  5. Plz answer this question ...
    By raghu2114 in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 07:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •