# 1.356f+1.265f=2.6209998

• March 13th, 2011, 07:06 AM
anshu4u
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.
Code Java:

```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?
• March 13th, 2011, 09:36 AM
helloworld922
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)