Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] creating a pointcut for primitive operations

Hi Andrew,


I’m sort of an AOP newbie but I had a question related to creating pointcuts which are not specifically for methods. The issue here is that java decimal arithmetic is NOT entirely accurate due to rounding so when you do something like 33 * 100 you get 329.999999945. What I’d like to be able to do is look for a pattern of x * y and replace it with roundIt(x*y). Does this sort of functionality exist or does anyone have a better idea?

It shouldn't be too difficult to extend AspectJ to have a join point for
operations like the multiplication. This would probably cause problems
with types which are usually cast implicitely (i.e. you might have to
deal separately with int*double, int*int, byte*double, etc...), but it
might work.

However, I don't think this would solve your problem at all.
The problem of decimal arthimetic is much more complex than this.
If you want 33*100 to be exactly 3300, use integers. If you want to keep
some decimal precision, keep all your decimal numbers in a decimal
representation, no floating points.
If you use floating point representations, the simple fact of assigning
33 (or any other number) to a double already makes it loose some
precision. It's a general fact regarding floating point operations: you
have to worry about the error margin as well (whereas the biggest
problem with int operations is probably limited to overflows).
Different processors have different error margins on floating point
operations. Originally, Java was meant to be independent of the
platform. However, not using the hardware floating point operations gave
quite poor performance. Since Java 1.2 (I think), the default behaviour
is to use the hardware operation for fp operations: you will get a
different rounding error depending on which processor you are using
(unless 'strictfp' is used).

In your case, you could use java.math.BigDecimal. It would then be
possible to use aspects around the operations to control the degree of
precision you are interested in.

This page is probably relevant: http://www2.hursley.ibm.com/decimal/


Cheers,

Bruno.



Back to the top