The Artima Developer Community
Sponsored Link

Java Answers Forum
trick question about float in java

6 replies on 1 page. Most recent reply: Jun 6, 2006 12:25 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Alex Tanil

Posts: 10
Nickname: alextanil
Registered: May, 2006

trick question about float in java Posted: May 5, 2006 9:04 AM
Reply to this message Reply
Advertisement
i was told to make a class for money, that makes objects like this. EUR+3.33. But i was told not to save 3.33 as a float but the integer and decimal part separate. I was asked why is not a good idea to use a float. and to be helped i was told that in cobol i could use BCD binary coded decimal, although its greedy.
A need an answer to this question but i cannot find one...


Alex Tanil

Posts: 10
Nickname: alextanil
Registered: May, 2006

Re: trick question about float in java Posted: May 5, 2006 9:14 AM
Reply to this message Reply
what is bigDecimal?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: trick question about float in java Posted: May 8, 2006 12:13 AM
Reply to this message Reply
Double and espescially float are not able to store exact numbers. They only store 'floating' numbers (Double just has a higher precision).

They store something like
a * e^b.
This has 2 effects:
1. The smaller the value, the higher the precision. but you loose precision for higher values.
2. Only a few numbers can be stored exactly, all others are stored as close as possible to the original value.
For finances this can make a difference.

Decimal and BigDecimal use another approach.
They store every digit as a separate value.
So 3.33 Is storeed as [3], [3] [3] with certain formatting informations. For calculations the value must be converted to Double of course. The advantage of BigDecimal is, that you can be sure that the number is exactly what you see on the screen.

Alex Tanil

Posts: 10
Nickname: alextanil
Registered: May, 2006

Re: trick question about float in java Posted: May 8, 2006 5:53 AM
Reply to this message Reply
thnx very much for the answers,i realy need them
i have so many questions.... that my head hurts!

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: trick question about float in java Posted: May 11, 2006 6:54 AM
Reply to this message Reply
> Double and espescially float are not able to store exact
> numbers. They only store 'floating' numbers (Double just
> has a higher precision).

That's not really accurate. They can store exact numbers. For example, 1/2 can be represented as float exactly. They are just in binary. The problem is that a lot of numbers that we can represent exactly in decimal (such as 1/10) are repeating numbers in binary fractions in the same way that we cannot represent 1/3 exactly in decimal.

The issue you go on to explain is with precision and may or may not come into play.

Erik Engbrecht

Posts: 210
Nickname: eengbrec
Registered: Apr, 2006

Re: trick question about float in java Posted: Jun 1, 2006 4:15 PM
Reply to this message Reply
>
> Decimal and BigDecimal use another approach.
> They store every digit as a separate value.
> So 3.33 Is storeed as [3], [3] [3] with certain formatting
> informations. For calculations the value must be converted
> to Double of course. The advantage of BigDecimal is, that
> you can be sure that the number is exactly what you see on
> the screen.

Two Things:
1. BigDecimal technically is not stored as a series of decimal digits. It is actually stored as a BigInteger (which is an array of integers) and an integer representing the location of the decimal place. Storing it as a decimal numbers instead of as binary numbers would be both wasteful and computationally inefficient. However, conceptually it can be thought of as storing each digit, so this is kind of a nit.

2. Converting a BigDecimal to a double for computation would entirely defeat the purpose of using a BigDecimal number. You would loose the precision of BigDecimal during the operation.

Take a look at the source for BigDecimal and BigInteger.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: trick question about float in java Posted: Jun 6, 2006 12:25 AM
Reply to this message Reply
1. Didn't say anything about the way it was implemented, just about the idea behind it.

2. Didn't know you can actually multiply two BigDecimal numbers without converting them first. SO i'll just have to take alook atthe code.

Flat View: This topic has 6 replies on 1 page
Topic: Object File Input/Array problem Previous Topic   Next Topic Topic: BIRT Report Problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use