Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Adding two float values got strange result

Reply
Thread Tools

Adding two float values got strange result

 
 
Pietro Marrone
Guest
Posts: n/a
 
      04-17-2007
Hi, thie is my simple test case:

public class Main {

public static void main(String[] args) {
float a = 123.76f;
float b = 52.0f;

System.out.println(a + b);
System.out.println((float) a + b);
System.out.println((float) a + (float) b);
System.out.println((float) (a + b));
}
}

The results for all four System.out is the same, guess what?:
175.01001

Cuold anyone explein me why, at least a refer to rules explaining what
happens?

Regards

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      04-17-2007
Pietro Marrone wrote:

> On 17 Apr, 11:50, Pietro Marrone <pietromarr...@gmail.com> wrote:
>> Hi, thie is my simple test case:
>>
>> public class Main {
>>
>> public static void main(String[] args) {
>> float a = 123.76f;
>> float b = 52.0f;
>>
>> System.out.println(a + b);
>> System.out.println((float) a + b);
>> System.out.println((float) a + (float) b);
>> System.out.println((float) (a + b));
>> }
>>
>> }
>>
>> The results for all four System.out is the same, guess what?:
>> 175.01001
>>
>> Cuold anyone explein me why, at least a refer to rules explaining what
>> happens?


Floating. Point. Numbers. Have. Limited. Precision. And. Are. Not.
Stored. As. Decimal. Values.

--
"Possibly you're not recalling some of his previous plans." Zoe, /Firefly/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

 
Reply With Quote
 
 
 
 
Pietro Marrone
Guest
Posts: n/a
 
      04-17-2007
On 17 Apr, 11:50, Pietro Marrone <pietromarr...@gmail.com> wrote:
> Hi, thie is my simple test case:
>
> public class Main {
>
> public static void main(String[] args) {
> float a = 123.76f;
> float b = 52.0f;
>
> System.out.println(a + b);
> System.out.println((float) a + b);
> System.out.println((float) a + (float) b);
> System.out.println((float) (a + b));
> }
>
> }
>
> The results for all four System.out is the same, guess what?:
> 175.01001
>
> Cuold anyone explein me why, at least a refer to rules explaining what
> happens?
>
> Regards


Another wrong test case:
package it.test;


public class Main {

public static void main(String[] args) {
float a = (float)123.76;
float b = (float)52.0;

System.out.println(a + b);
System.out.println((float) a + b);
System.out.println((float) a + (float) b);
System.out.println((float) (a + b));
}
}

gives 175.76001, but unfortunetly neither this result is correct.

My JRE version is:
java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode)

My OS is: Windows XP Professional Version 2002 SP2

My Hardware is:
AMD Athlon 64 Processor 3500+

Regards

 
Reply With Quote
 
Pietro Marrone
Guest
Posts: n/a
 
      04-17-2007
On 17 Apr, 11:50, Pietro Marrone <pietromarr...@gmail.com> wrote:
> Hi, thie is my simple test case:
>
> public class Main {
>
> public static void main(String[] args) {
> float a = 123.76f;
> float b = 52.0f;
>
> System.out.println(a + b);
> System.out.println((float) a + b);
> System.out.println((float) a + (float) b);
> System.out.println((float) (a + b));
> }
>
> }
>
> The results for all four System.out is the same, guess what?:
> 175.01001
>
> Cuold anyone explein me why, at least a refer to rules explaining what
> happens?
>
> Regards


It works if I declare primitive types as double.
iis it a strange causality or is a rule?

Regards

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      04-17-2007
Pietro Marrone wrote:
..
>public class Main {
>
> public static void main(String[] args) {
> float a = 123.76f;
> float b = 52.0f;
>
> System.out.println(a + b);
> System.out.println((float) a + b);
> System.out.println((float) a + (float) b);
> System.out.println((float) (a + b));
> }
>}
>
>The results for all four System.out is the same, guess what?:
>175.01001


The results I get here are..

175.76001
175.76001
175.76001
175.76001

>Cuold anyone explein me why, at least a refer to rules explaining what
>happens?


Buggy implementation?
Check the launch files here (not the
screenshot on the right).
<http://www.physci.org/jws/#jtest>
..what is your java.vm.version (1.6.0-b105
in the screenshot)?

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via http://www.javakb.com

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-17-2007
Pietro Marrone wrote:
> Hi, thie is my simple test case:
>
> public class Main {
>
> public static void main(String[] args) {
> float a = 123.76f;
> float b = 52.0f;
>
> System.out.println(a + b);
> System.out.println((float) a + b);
> System.out.println((float) a + (float) b);
> System.out.println((float) (a + b));
> }
> }
>
> The results for all four System.out is the same, guess what?:
> 175.01001


Are you absolutely sure the program you posted got that result?

I ran it with result:

175.76001
175.76001
175.76001
175.76001

which is the same as Andrew got. All Java implementations should get the
same answer for the posted program.

Patricia
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-17-2007
Pietro Marrone wrote:
> On 17 Apr, 11:50, Pietro Marrone <pietromarr...@gmail.com> wrote:
>> Hi, thie is my simple test case:
>>
>> public class Main {
>>
>> public static void main(String[] args) {
>> float a = 123.76f;
>> float b = 52.0f;
>>
>> System.out.println(a + b);
>> System.out.println((float) a + b);
>> System.out.println((float) a + (float) b);
>> System.out.println((float) (a + b));
>> }
>>
>> }
>>
>> The results for all four System.out is the same, guess what?:
>> 175.01001
>>
>> Cuold anyone explein me why, at least a refer to rules explaining what
>> happens?
>>
>> Regards

>
> Another wrong test case:
> package it.test;
>
>
> public class Main {
>
> public static void main(String[] args) {
> float a = (float)123.76;
> float b = (float)52.0;
>
> System.out.println(a + b);
> System.out.println((float) a + b);
> System.out.println((float) a + (float) b);
> System.out.println((float) (a + b));
> }
> }
>
> gives 175.76001, but unfortunetly neither this result is correct.


The result you report for this program matches the answer Andrew and I
got for the first program.

Java float and double each represent numbers in binary floating point.
Java float has, effectively, 24 significant bits, the equivalent of
about 7.2 decimal digits. You should expect rounding error in the 8th
significant decimal digit.

The rules are in the JLS, see
http://java.sun.com/docs/books/jls/t...ues.html#4.2.3
the following section, and the sections they reference.

IEEE 32 bit float is almost always the wrong data type to use. It gives
up a lot of accuracy for halving the space required compared to double.
If you should be using a binary floating point type at all, you should
normally use double. The exception is if you are storing a large number
of values with low accuracy requirements, and have done the numerical
analysis to convince yourself that float is precise enough.

If you need the behavior you seem to expect, exact representation and
addition of decimal fractions, consider using BigDecimal.

Patricia

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-17-2007
Pietro Marrone wrote:
>>> public class Main {
>>>
>>> public static void main(String[] args) {
>>> float a = 123.76f;
>>> float b = 52.0f;
>>>
>>> System.out.println(a + b);
>>> System.out.println((float) a + b);
>>> System.out.println((float) a + (float) b);
>>> System.out.println((float) (a + b));
>>> }
>>>
>>> }
>>>
>>> The results for all four System.out is the same, guess what?:
>>> 175.01001
>>>
>>> Cuold anyone explein me why, at least a refer to rules explaining what
>>> happens?


Chris Dollin wrote:
> Floating. Point. Numbers. Have. Limited. Precision. And. Are. Not.
> Stored. As. Decimal. Values.


Why isn't numerical analysis part of developer training?

To the OP:
<http://developers.sun.com/sunstudio/numerics_index.html>

In particular:
<http://docs.sun.com/source/817-6702/ncg_goldberg.html>

--
Lew
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      04-17-2007
Lew wrote:

> Chris Dollin wrote:
>> Floating. Point. Numbers. Have. Limited. Precision. And. Are. Not.
>> Stored. As. Decimal. Values.

>
> Why isn't numerical analysis part of developer training?


Because it's a horribly complex and detailed subject, most of which
is irrelevant to most of what most developers do.

Some /basic awareness/ of the floaty bits of floating point should
be grafted into the brain of a putative developer, mind.

--
"It took a very long time, much longer than the most /Sector General/
generous estimates." - James White

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-17-2007
Pietro Marrone wrote:
> On 17 Apr, 11:50, Pietro Marrone <pietromarr...@gmail.com> wrote:
>> Hi, thie is my simple test case:
>>
>> public class Main {
>>
>> public static void main(String[] args) {
>> float a = 123.76f;
>> float b = 52.0f;
>>
>> System.out.println(a + b);
>> System.out.println((float) a + b);
>> System.out.println((float) a + (float) b);
>> System.out.println((float) (a + b));
>> }
>>
>> }
>>
>> The results for all four System.out is the same, guess what?:
>> 175.01001
>>
>> Cuold anyone explein me why, at least a refer to rules explaining what
>> happens?
>>
>> Regards

>
> It works if I declare primitive types as double.
> iis it a strange causality or is a rule?
>
> Regards
>


As a general rule, double gives a lot more precision than float, 53
significant bits instead of 24. However, the default Double.toString
conversion always produces enough decimal places to uniquely identify
the double value, so it can produce similar effects, just with longer
output. Of course, all of this is very dependent on the particular
numbers you pick.

If you strongly favor exact processing of decimal fractions, you should
use neither double nor float, but BigDecimal which is designed for that job.

Patricia
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exclusive float range, Range#step result in counterintuitive result Joey Zhou Ruby 5 04-15-2011 02:22 AM
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Float to int conversion by using two int variables for representation of the float variable k3n3dy C++ 15 04-20-2006 06:53 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57