• Post Reply Bookmark Topic Watch Topic
  • New Topic

Float addition giving wrong value  RSS feed

 
Sahana Hegde
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I have initialized 2 float values


This results in 40865.453 instead of 40865.45.
Am I doing something wrong?

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. This is not even a java problem. computers have a hard time holding the EXACT float value you want... but they get pretty close. I don't have the spec handy (and you don't REALLY want to read it anyway) but there is an IEEE standard on how floats are stored.

generally, when using floats, you're going to do your math, then format the result to the rounding you want... or check that the result is within whatever tolerances you consider acceptable.
[ October 16, 2008: Message edited by: fred rosenberger ]
 
Sahana Hegde
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. So each time I add I have to round it to the nearest value is it?

I tried the same with Double it gives the correct result. (ends with .45)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Essential reading: Some things you should know about floating-point arithmetic.
 
Sahana Hegde
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link.
Suppose I have to create about 30 BigDecimal variables in a single jsp page,
will it be bad in terms of performance?

Thanks
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you shouldn't be creating any Java variables in a JSP page, from the design point of view.

But 30 BigDecimal objects is a very small number. Computers are fast today, but it wouldn't have been a big number ten years ago either. Try it if you like. You won't notice any difference even if you create 3,000 BigDecimal objects, I predict.
 
Sahana Hegde
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. Thanks.

Yeah from a design point of view its bad to put it in a jsp page. But a lot of code is generating the HTML and it would be tedious to do in in a servlet.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul is right.

In itself, creating numerous objects is not the resource hit a lot of people expect. Behind the scenes, your Java programs are probably creating far more objects than you realize. For example, simply calling Integer.valueOf(0) creates a cache array of 256 new Integer objects.

There would be some performance cost in creating BigDecimal instances. But whether it would be "bad" (noticible) depends more on what you do with these objects.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sahana Hegde:
Yeah from a design point of view its bad to put it in a jsp page. But a lot of code is generating the HTML and it would be tedious to do in in a servlet.
Right. And having a servlet generate HTML is also a bad thing to do, too, from the design point of view. What you should do is to have the servlet organize the data that is to be sent to the client, put that data into request scope, then forward to a JSP that takes the data and writes the HTML containing it. This pattern has been around for at least 5 years, so it surprises me that there are still people who don't use it.
 
Sahana Hegde
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am generating a report with the data and I calculating in each loop of the result set the data that needs to be generated, and there is a whole lot of calculations with multiple records before I can generate one row in the report
What should be the approach then?

Also could you guys suggest some material for reading for best practices in building applications with simple things like using the right data type to some more complex things.

Thanks a lot for all your replies.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disadvantage: 10 years old, printed before Swing, the Collections Framework, the invention of Agile Programming and the discovery that Thread#stop doesn't work.
Advantage: easy to read, available second-hand for a very reasonable price:
S Gilbert, M McCarty, Object-Oriented Design in Java�, Corte Madera CA: Waite Group Press (MacMillan) (1998).

It doesn't however seem to mention the imprecision of floating-point arithmetic. Some of my programming books mention this, eg Deitel and Deitel, 6th edition page 190, and Barry Burd, "Beginning Programming with Java for Dummies" (page 300) has a listing with "Do you have change for 20.338500000000003" written against it.
 
Sahana Hegde
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the info.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sahana Hegde:
Thanks for the reply. So each time I add I have to round it to the nearest value is it?

I tried the same with Double it gives the correct result. (ends with .45)


That is because double is more precise than float. But double suffers from the same round-of errors. Run this example:

[ October 17, 2008: Message edited by: Piet Verdriet ]
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahana Hegde, you're welcome.
Piet: it worked beautifully . . . once I lost the extraneous Exception declaration
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
...
Piet: it worked beautifully . . . once I lost the extraneous Exception declaration


Hehe, yeah, before the floating-point demo, there was a java.util.Scanner-demo which (could) throw an IOException. Forgot to remove it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!