Friday, September 19, 2008

Beware CGAffineTransformScale() negative zero


#import <ApplicationServices/ApplicationServices.h>

int main(int argc,char *argv){
CGAffineTransform matrix;

matrix=CGAffineTransformMakeTranslation(42,42);
matrix=CGAffineTransformConcat(CGAffineTransformMakeScale(1,-1),matrix);

printf("matrix=%g %g %g %g %g %g\n",matrix.a,matrix.b,matrix.c,matrix.d,matrix.tx,matrix.ty);

matrix=CGAffineTransformMakeTranslation(42,42);
matrix=CGAffineTransformScale(matrix,1,-1);

printf("matrix=%g %g %g %g %g %g\n",matrix.a,matrix.b,matrix.c,matrix.d,matrix.tx,matrix.ty);

}

Generates:

matrix=1 0 0 -1 42 42
matrix=1 0 -0 -1 42 42


Yes, that is a negative zero.

While zero and negative zero are considered equal and produce the same results in most situations, there are cases where negative zero will give different results. e.g. 1.0 / 0.0 is positive infinity (greater than zero) and 1.0 / -0.0 is negative infinity (less than zero).

(OSX 10.5.4)