Velocity Reviews > Java > Question on additional decimals in implicit conversion

# Question on additional decimals in implicit conversion

Jacob
Guest
Posts: n/a

 10-02-2003
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
Guest
Posts: n/a

 10-02-2003
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
..100000000000000005551115123125782702118158340454 1015625
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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

Jon Skeet
Guest
Posts: n/a

 10-02-2003
Jacob <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet

Guest
Posts: n/a

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

Roedy Green
Guest
Posts: n/a

 10-02-2003
On Thu, 02 Oct 2003 16:52:26 GMT, "The Law of Lorentz"
<(E-Mail Removed)> wrote or quoted :

>double val = .1;
>//'val' is not exactly equal to .1, but it is actually equal to
>.100000000000000005551115123125782702118158340454 1015625
>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.

Michael Borgwardt
Guest
Posts: n/a

 10-02-2003
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.

Jon Skeet
Guest
Posts: n/a

 10-03-2003
Michael Borgwardt <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet

Dale King
Guest
Posts: n/a

 10-03-2003

http://citeseer.nj.nec.com/goldberg91what.html

--
Dale King