Forums Register Login

Float addition giving wrong value

+Pie Number of slices to send: Send
Hi Guys,

I have initialized 2 float values


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

Thanks
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
Essential reading: Some things you should know about floating-point arithmetic.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thank you for the info.
+Pie Number of slices to send: Send
 

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 ]
+Pie Number of slices to send: Send
Sahana Hegde, you're welcome.
Piet: it worked beautifully . . . once I lost the extraneous Exception declaration
+Pie Number of slices to send: Send
 

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.
Can you smell this for me? I think this tiny ad smells like blueberry pie!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 7695 times.
Similar Threads
wrapper class
Understanding NaN
Equals doubt
equals method behavior of Float
About default values in array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:43:38.