[ImageJ-devel] Question regarding shortcut assignment operator in Java...

Rick Lentz rwlentz at wisc.edu
Mon Jun 14 11:49:18 CDT 2010


Greetings,

  I was running up against several 'bugs' in my implementations of NIO
backed arrays for the ImageJ effort.  I have identified the source of
the bugs.  It appears that the assignment operator '*=' in Java
produces different results for some numbers in comparison to the
expanded version of the notation.  This was surprising to me since I
assumed that use of the shortcut assignment operator in Java (E.g. A
*= B ) would produce the same results as A = A * B.

  Although the initial difference is small, when carried forward for
30 steps over hundreds of iterations... the error (carried forward?)
becomes considerable.

  Here is a snippet that demos the error (all data is float):

//expanded notation
float ad = data2[k][i + w*j] * gka[k]*gja[j]*gia[i];
//existing implementation
data2[k][i + w*j] *= gka[k]*gja[j]*gia[i];
//result of shortcut notation
float ac = data2[k][i + w*j];
//direct comparison
if(ad != ac)
{
	System.out.println("Value at " + (k) + " , " + (i + w*j) + " of " +
ac + " != " + ad );
}
//input values
Value at 0 , 133 of 1.429051E-4 != 1.4290512E-4
Value at 0 , 133 of 1.429051E-4 not = value at 133 of 1.4290512E-4
Component values are 1.0 for gk
Component values are 0.99993896 for gj
Component values are 0.99847525 for gi
Component values are 1.4313207E-4 for data2

 I am leaning towards standardizing the reference implementations to
replace instances of *= with the expanded notation to obtain
consistent results between imagelib NIO types and existing code.  I am
open to feedback regarding this issue and certainly hope that I am
simply overlooking something trivial.

Sincerely,

Rick Lentz
Bascom Hall - "...ever encourage that continual and fearless sifting
and winnowing by which alone the truth can be found"




More information about the ImageJ-devel mailing list