Bug 28615

Summary: Cannot optimize out -0.0 in array initializers
Product: [Eclipse Project] JDT Reporter: Olivier Thomann <Olivier_Thomann>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 2.1   
Target Milestone: 2.1 M5   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

Description Olivier Thomann CLA 2002-12-18 09:58:23 EST
Using 1217, the following code doesn't return the right result when executed.
public class A {
    public static void main(String[] args) {
        double[] tab = new double[] {-0.0};
        System.out.print(tab[0]);
    }
}
When executed, we return: 0.0 instead of -0.0.

javac and jikes returns the right result.

The bug comes from the optimization for array initializers that optimizes out
the default values. We should check if the double value is equals to -0.0 or
different from 0.0 before we optimize out the constant. For now we simply check
different from 0.
Here is a suggested fix for this:

In org.eclipse.jdt.internal.compiler.ast.ArrayInitializer::generateCode:

	case T_int :
	case T_short :
	case T_byte :
	case T_char :
	case T_float :
	case T_long :
	case T_double :
		if (expr.constant.doubleValue() != 0) {
			codeStream.dup();
			codeStream.generateInlinedValue(i);
			expr.generateCode(currentScope, codeStream, true);
			codeStream.arrayAtPut(elementsTypeID, false);
		}
	break;

with:
	case T_int :
	case T_short :
	case T_byte :
	case T_char :
	case T_long :
		if (expr.constant.longValue() != 0) {
			codeStream.dup();
			codeStream.generateInlinedValue(i);
			expr.generateCode(currentScope, codeStream, true);
			codeStream.arrayAtPut(elementsTypeID, false);
		}
         break;
	case T_float :
	case T_double :
		double constantValue = expr.constant.doubleValue();
		if (constantValue == -0.0 || constantValue != 0) {
			codeStream.dup();
			codeStream.generateInlinedValue(i);
			expr.generateCode(currentScope, codeStream, true);
			codeStream.arrayAtPut(elementsTypeID, false);
		}
	break;

This would fix the same bug for float constants.

With this patch, we have the right result.
Comment 1 Philipe Mulet CLA 2002-12-19 06:15:23 EST
Ok by me.
Comment 2 Olivier Thomann CLA 2003-01-06 08:51:01 EST
Fixed and released in 2.1 stream.
Regression tests added.
Comment 3 David Audel CLA 2003-02-07 11:44:23 EST
Verified.