Question on additional decimals in implicit conversion

J

Jacob

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
 
T

The Law of Lorentz

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
 
J

Jon Skeet

Jacob said:
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
 
S

Sajjad Lateef

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.
 
R

Roedy Green

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.
 
M

Michael Borgwardt

Jacob said:
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.
 
J

Jon Skeet

Michael Borgwardt said:
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.
 

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

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top