This identity happens to hold when computed as written in IEEE-754 double precision. Here's why:
The square root of two correctly rounded to double precision is:
sqrt(2) = 0x1.6a09e667f3bcd * 2^0
(I'm using hexadecimal here because the representations are tidier, and the translation into the IEEE754 format is much easier). Multiplication by two is exact in binary floating-point if no overflow occurs, as in this case here, so:
2*sqrt(2) = 0x1.6a09e667f3bcd * 2^1
When we add three, we get:
3 + 2*sqrt(2) = 0x1.7504f333f9de68 * 2^2
This, however, is not a representable double-precision number (it is one bit too wide), so the result is rounded to the nearest representable number. It happens that this value is exactly halfway between two representable numbers, so we pick the one with a trailing zero bit:
3 + 2*sqrt(2) = 0x1.7504f333f9de6 * 2^2
Now the other side of the computation. When we add one to the double-precision square root of two, we get:
1 + sqrt(2) = 0x1.3504f333f9de68 * 2^1
This is also an exact halfway case between to representable double-precision numbers, and again it is rounded to the nearest "even" representable number:
1 + sqrt(2) = 0x1.3504f333f9de6 * 2^1
When this value is squared, the result is:
(1 + sqrt(2))*(1 + sqrt(2)) = 0x1.7504f333f9de599cacbc97eaa4 * 2^2
Which is not a representable double-precision number either. This one is not an exact halfway case, so it merely rounds to the nearest representable number, which is:
(1 + sqrt(2))*(1 + sqrt(2)) = 0x1.7504f333f9de6 * 2^2
Summary: Computing this value in two different ways incurs two different sequences of roundings, but the final result is the same. We only looked at the computation in double precision, however; this may not be the case when the computation is carried out using different arithmetic types.
In general, however, the expression 3 + 2*sqrt(2)
should be expected to be the more accurate (in cases where they differ), because it incurs only two roundings (the square root and the add) for any binary IEEE-754 type, whereas (1 + sqrt(2))*(1 + sqrt(2))
incurs three roundings (square root, add, and multiply). It should also be noted that the difference between the two will be at most one or two bits, and is probably negligable for your purposes.