Question on additional decimals in implicit conversion

Discussion in 'Java' started by Jacob, Oct 2, 2003.

  1. Jacob

    Jacob Guest

    Hello everyone,

    I am teaching Java for the first time this semester and I'm looking
    for an explanation to a problem I have encountered. My students were
    working with dollar amounts stored as floats and they needed to get
    the whole dollar amount isolated from the change. I suggested they do

    float totalAmount,

    totalAmount = 5.08f;
    wholeDollars = (int) totalAmount;
    change = totalAmount - wholeDollars;

    It seems that this would work leaving change to be 0.08 - but instead
    change has the value 0.07999992 which cause some calculations to go
    wrong later on. I understand that this has to do with casting the
    float as an integer but I'm wondering what is actually going on (at
    the bit level I guess?) that causes the result to come out this way?

    Jacob, Oct 2, 2003
    1. Advertisements

  2. For precise calculation you have to use "BigDecimal" class, infact double
    and float are faster but not precisely.
    For example:
    double val = .1;
    //'val' is not exactly equal to .1, but it is actually equal to
    This is so because .1 cannot be represented exactly as a double (or, for
    that matter, as a binary fraction of any finite length).

    I hope my suggestion is useful for you
    The Law of Lorentz, Oct 2, 2003
    1. Advertisements

  3. Jacob

    Jon Skeet Guest

    It doesn't actually have a lot to do with casting - it has everything
    to do with floating point arithmetic though. I have some pages on
    floating point in .NET, and the types are very similar in Java, so you
    may find the following page useful:
    Jon Skeet, Oct 2, 2003
  4. Reference:
    ANSI/IEEE Standard 754-1985 Standard for Binary Floating Point Arithmetic

    Computer Organization and Architecture,
    William Stallings, pp. 222-234
    Macmillan Publishing Company, ISBN 0-02-415480-6

    Computer Architecture: A Quantative Approach,
    D. A. Patterson and J. L. Hennessy,
    Morgan Kaufmann Publishers, Inc., San Mateo, CA, 1996.
    (See the Appendix on Floating point numbers)

    For most fun, look up:
    Hacker's Delight
    Henry S. Warren Jr.
    ISBN 0201914654 Addison-Wesley, 2003

    Warren has an entire chapter devoted to this issue.
    Sajjad Lateef, Oct 2, 2003
  5. Jacob

    Roedy Green Guest

    Another way to think of this is float is accurate to about 6 to 7
    digits and double 14 to 15 digits. Though IEEE now guarantees some
    precise values, you will never get in trouble if you assume they are
    only accurate to that degree of precision.
    Roedy Green, Oct 2, 2003
  6. And that's where you go wrong. For the resons explained by the other
    posters, floating point variables should never be used for money, in
    any language. At leat not in a serious application. Of course it
    matters little when you're just teaching the language basics, but
    you should at least tell your students that it's not exactly best
    Michael Borgwardt, Oct 2, 2003
  7. Jacob

    Jon Skeet Guest

    It may be okay to use floating point variables - just not floating
    *binary* point variables. Floating *decimal* point variables may well
    be appropriate, with a few extra rules etc.
    Jon Skeet, Oct 3, 2003
  8. Jacob

    Dale King Guest

    Dale King, Oct 3, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.