gebruik van strictfp

Status
Niet open voor verdere reacties.

Clemens Schalkw

Gebruiker
Lid geworden
5 dec 2007
Berichten
166
Ik heb zojuist een nieuw keyword van Java "geleerd".
Is er iemand die dit wel eens gebruikt en zo ja, kun je daar dan een goed voorbeeld van geven ?

Op de site van Sun staat iets als:

Code:
public class MyClass {

    public static void main(String[] args) {
        Double x = 2.0;
        Double max = Double.MAX_VALUE - 1;
        Double crash = x * max / 2;

        System.out.println(crash);
   }
}

Als je je class strictfp maakt, doet hij wel de / 2 waardoor je weer een geldige Double krijgt.
 
Laatst bewerkt:
Nog nooit nodig gehad... het nut lijkt me beperkt, aangezien er een BigDecimal klasse bestaat. Op die manier krijg je eveneens op elk platform dezelfde resultaten.

Hetzelfde voorbeeldje:
Code:
BigDecimal bdMax = new BigDecimal(Double.MAX_VALUE - 1);
bdMax = bdMax.multiply(new BigDecimal("2"));
bdMax = bdMax.divide(new BigDecimal("2"));
System.out.println(bdMax);

Wat neerkomt op een slordige 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368

Ik krijg overigens 'Infinity' met je voorbeeldje, zowel met als zonder strictfp. Misschien heb'k iets over het hoofd gezien.
 
Ja, slordig voorbeeld inderdaad van mij. Ik had beter gewoon de link naar het Sun voorbeeld kunnen posten, of het voorbeeld kunnen overtypen :p.

Hier dan alsnog: (en de link)

Code:
    public strictfp class FpDemo3 {
        public static void main(String[] args) {
            double d = 8e+307;
            System.out.println(4.0 * d * 0.5);
            System.out.println(2.0 * d);
        }
    }

The maximum value of a double (Double.MAX_VALUE) is approximately 1.8e+308. In the FpDemo3 example, the first expression is evaluated as:

(4.0 * d) * 0.5

because the Java programming language guarantees a left-to-right order of evaluation. When the first part of the expression is evaluated, the result is 32e+307 or 3.2e+308, which is larger than Double.MAX_VALUE.

Because the expression is FP-strict, the implementation is required to evaluate the whole expression as resulting in positive infinity (which the program prints as "Infinity"). This is true even though the later multiplication by 0.5 produces a final value for the expression of 1.6e+308, which is less than Double.MAX_VALUE.

By contrast, if the expression is not FP-strict, an implementation is allowed to use an extended exponent range to represent intermediate results. In the FpDemo3 example, this could keep the expression from overflowing, and produce a final result that is within range. An implementation is not required to do this; it can, in effect, use FP-strict rules everywhere.
 
Dat is ook de uitleg die'k elders las... dan klopt wat je zegt in je eerste post toch niet? Strictfp dient om op elk platform hetzelfde resultaat te krijgen. Als je de berekening strictfp maakt dan wordt elke tussenstap een fp.

Because the expression is FP-strict, the implementation is required to evaluate the whole expression as resulting in positive infinity (which the program prints as "Infinity"). This is true even though the later multiplication by 0.5 produces a final value for the expression of 1.6e+308, which is less than Double.MAX_VALUE.

Is net het tegenovergestelde van wat in je eerste post staat:

... Als je je class strictfp maakt, doet hij wel de / 2 waardoor je weer een geldige Double krijgt.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan