Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: 1.356f+1.265f=2.6209998

1. ## 1.356f+1.265f=2.6209998

While coding i got some unexpected output. like in the following code when i give this two 1.356 1.265 i got he result as 2.6209998. there r error like this for mane pair of number right now i cant remember.
```class Sumfault
{
public static void main(String str[])
{
float f1,f2;
f1=Float.parseFloat(str[0]);
f2=Float.parseFloat(str[1]);
System.out.println("Result is: "+(f1+f2));
}
}```
But can anybody explain why this is happening?

2. ## Re: 1.356f+1.265f=2.6209998

The reason it does that is because of the way floating point numbers are represented. If you want to really understand why it's that result, you can look at Floating point - Wikipedia, the free encyclopedia. Basically it boils down to floating points not being able to exactly represent 1.356f or 2.621f exactly, and because computers almost always use truncation instead of rounding, you get a number smaller than 2.621f (the actual answer).

1.356f = 0x3FAD9168 = 1.3559999 (slightly smaller than 1.356)
1.265f = 0x3FA1EB85 = 1.2650000 (exact)
2.621f = 0x4027BE76 = 2.6209998 (slightly smaller than 2.621)

3. ## The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

anshu4u (March 14th, 2011), JavaPF (March 14th, 2011)