# Question on additional decimals in implicit conversion

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

1. ### JacobGuest

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

2. ### The Law of LorentzGuest

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

3. ### Jon SkeetGuest

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

Jon Skeet, Oct 2, 2003

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.

Warren has an entire chapter devoted to this issue.

5. ### Roedy GreenGuest

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.

--
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
6. ### Michael BorgwardtGuest

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
7. ### Jon SkeetGuest

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