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
    this:

    float totalAmount,
    wholeDollars,
    change;

    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?

    Thanks,
    Jacob
     
    Jacob, Oct 2, 2003
    #1
    1. Advertising

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



    "Jacob" <> wrote in message
    news:...
    > 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
    > this:
    >
    > float totalAmount,
    > wholeDollars,
    > change;
    >
    > 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?
    >
    > Thanks,
    > Jacob
     
    The Law of Lorentz, Oct 2, 2003
    #2
    1. Advertising

  3. Jacob

    Jon Skeet Guest

    Jacob <> wrote:
    > 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
    > this:
    >
    > float totalAmount,
    > wholeDollars,
    > change;
    >
    > 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?


    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:

    http://www.pobox.com/~skeet/csharp/floatingpoint.html

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Oct 2, 2003
    #3
  4. On Thu, 02 Oct 2003 08:42:00 -0700, Jacob wrote:

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


    Reference:
    ANSI/IEEE Standard 754-1985 Standard for Binary Floating Point Arithmetic

    Books:
    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
    #4
  5. Jacob

    Roedy Green Guest

    On Thu, 02 Oct 2003 16:52:26 GMT, "The Law of Lorentz"
    <> wrote or quoted :

    >double val = .1;
    >//'val' is not exactly equal to .1, but it is actually equal to
    >.1000000000000000055511151231257827021181583404541015625
    >This is so because .1 cannot be represented exactly as a double (or, for
    >that matter, as a binary fraction of any finite length).


    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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    try http://24.87.56.253 if you have trouble getting through.
     
    Roedy Green, Oct 2, 2003
    #5
  6. Jacob wrote:
    > 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 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
    practice.
     
    Michael Borgwardt, Oct 2, 2003
    #6
  7. Jacob

    Jon Skeet Guest

    Michael Borgwardt <> wrote:
    > Jacob wrote:
    > > 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 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
    > practice.


    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 - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Oct 3, 2003
    #7
  8. Jacob

    Dale King Guest

    Dale King, Oct 3, 2003
    #8
    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. dmoos AT esigma-systems DOT de

    implicit vs. explicit type conversion for string vs. (char *)

    dmoos AT esigma-systems DOT de, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    2,537
    Default User
    Jun 26, 2003
  2. Russell Reagan

    Implicit conversion is evil?

    Russell Reagan, Oct 27, 2003, in forum: C++
    Replies:
    2
    Views:
    328
    Russell Reagan
    Oct 27, 2003
  3. al
    Replies:
    2
    Views:
    5,627
    Ernst Murnleitner
    Jan 7, 2004
  4. Boris
    Replies:
    3
    Views:
    421
  5. Ruby Stevenson

    Question on implicit type conversion

    Ruby Stevenson, Mar 7, 2012, in forum: C++
    Replies:
    2
    Views:
    329
    Richard Damon
    Mar 7, 2012
Loading...

Share This Page