You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by ad...@apache.org on 2008/02/10 14:01:08 UTC

svn commit: r620277 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java

Author: adelmelle
Date: Sun Feb 10 05:01:07 2008
New Revision: 620277

URL: http://svn.apache.org/viewvc?rev=620277&view=rev
Log:
Tweak: wrap numeric values internally in Integers or Longs if possible, Doubles only if necessary.

Modified:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java?rev=620277&r1=620276&r2=620277&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java Sun Feb 10 05:01:07 2008
@@ -61,7 +61,7 @@
             }
             Number val = p.getNumber();
             if (val != null) {
-                return new NumberProperty(val);
+                return getInstance(val.doubleValue());
             }
             return convertPropertyDatatype(p, propertyList, fo);
         }
@@ -74,40 +74,54 @@
     private final Number number;
 
     /**
-     * Constructor for Number input
-     * @param num Number object value for property
-     */
-    private NumberProperty(Number num) {
-        this.number = num;
-    }
-
-    /**
      * Constructor for double input
      * @param num double numeric value for property
      */
-    protected NumberProperty(double num) {
-        this.number = new Double(num);
+    private NumberProperty(double num) {
+        //Store the number as an int or a long,
+        //if possible
+        if ((num >= 0 && num == Math.floor(num))
+                || num == Math.ceil(num)) {
+            if (num < Integer.MAX_VALUE) {
+                this.number = new Integer((int)num);
+            } else {
+                this.number = new Long((long)num);
+            }
+        } else {
+            this.number = new Double(num);
+        }
     }
 
     /**
      * Constructor for integer input
      * @param num integer numeric value for property
      */
-    protected NumberProperty(int num) {
+    private NumberProperty(int num) {
         this.number = new Integer(num);
     }
     
     /**
      * Returns the canonical NumberProperty instance
      * corresponding to the given Number
-     * @param num   the base Number
+     * @param num   the base Double
      * @return  the canonical NumberProperty
      */
-    public static NumberProperty getInstance(Number num) {
+    public static NumberProperty getInstance(Double num) {
         return (NumberProperty)cache.fetch(
-                    new NumberProperty(num));
+                    new NumberProperty(num.doubleValue()));
     }
     
+    /**
+     * Returns the canonical NumberProperty instance
+     * corresponding to the given Integer
+     * @param num   the base Integer
+     * @return  the canonical NumberProperty
+     */
+    public static NumberProperty getInstance(Integer num) {
+        return (NumberProperty)cache.fetch(
+                    new NumberProperty(num.intValue()));
+    }
+
     /**
      * Returns the canonical NumberProperty instance
      * corresponding to the given double



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org


Re: svn commit: r620277 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java

Posted by Andreas Delmelle <an...@telenet.be>.
On Feb 11, 2008, at 19:26, Andreas Delmelle wrote:

<snip />
> So,
>
> Math.floor(-2.3) = -3
> Math.ceil(-2.3) = -2
> Math.rint(-2.3) = -2
>
> rint() has the additional side-effect of returning just the closest  
> even integer, so:
>
> Math.rint(-3.3) == Math.floor(-3.3) != Math.ceil(-3.3)

Correction, this would only holds for -3.5 because that is a value  
that is as close to -4 as it is to -3.


Re: svn commit: r620277 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java

Posted by Andreas Delmelle <an...@telenet.be>.
On Feb 11, 2008, at 19:26, Andreas Delmelle wrote:

>>
>> Err...
>> Math.floor(-2.0) == Math.ceil(-2.0) == Math.rint(-2.0) == -2.0
>
> Did you actually check that? In that case, your JVM would not be  
> compliant, I think...
>
> The API docs say:
>
> Math.floor(-2.3) = -3
> Math.ceil(-2.3) = -2
> Math.rint(-2.3) = -2

Sorry, must be the Monday...
Of course, we're both absolutely right, and for doubles that  
correspond to integers, what I wrote actually does not matter...


Cheers

Andreas

Re: svn commit: r620277 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java

Posted by Andreas Delmelle <an...@telenet.be>.
On Feb 11, 2008, at 18:05, Vincent Hennebert wrote:

Hi

> Andreas Delmelle wrote:
>> On Feb 11, 2008, at 11:23, Vincent Hennebert wrote:
>>
>>>> +        if ((num >= 0 && num == Math.floor(num))
>>>> +                || num == Math.ceil(num)) {
>>>
>>> Isn’t that the same as
>>>     if (num == Math.floor(num))
>>
>> Yes, for positive integers. For negative ints, it is 'num ==
>> Math.ceil(num)'.
>
> Err...
> Math.floor(-2.0) == Math.ceil(-2.0) == Math.rint(-2.0) == -2.0

Did you actually check that? In that case, your JVM would not be  
compliant, I think...

The API docs say:

floor() -> the largest (closest to positive infinity) double value  
that is not greater than the argument and is equal to a mathematical  
integer
ceil() -> the smallest (closest to negative infinity) double value  
that is not less than the argument and is equal to a mathematical  
integer
rint() -> the double value that is closest in value to the argument  
and is equal to a mathematical integer.


So,

Math.floor(-2.3) = -3
Math.ceil(-2.3) = -2
Math.rint(-2.3) = -2

rint() has the additional side-effect of returning just the closest  
even integer, so:

Math.rint(-3.3) == Math.floor(-3.3) != Math.ceil(-3.3)

As for my proposed alternative, that should obviously be:

Math.abs(num) == Math.floor(Math.abs(num))

Cheers

Andreas

Re: svn commit: r620277 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java

Posted by Vincent Hennebert <vi...@anyware-tech.com>.
Andreas Delmelle wrote:
> On Feb 11, 2008, at 11:23, Vincent Hennebert wrote:
> 
> Hi Vincent
> 
>> Hi Andreas,
>>
>>> Author: adelmelle
>>> Date: Sun Feb 10 05:01:07 2008
>>> New Revision: 620277
>> <snip/>
>>> +    private NumberProperty(double num) {
>>> +        //Store the number as an int or a long,
>>> +        //if possible
>>> +        if ((num >= 0 && num == Math.floor(num))
>>> +                || num == Math.ceil(num)) {
>>
>> Isn’t that the same as
>>     if (num == Math.floor(num))
> 
> Yes, for positive integers. For negative ints, it is 'num ==
> Math.ceil(num)'.

Err...
Math.floor(-2.0) == Math.ceil(-2.0) == Math.rint(-2.0) == -2.0

And same for a positive double. No need to make the distinction.

Vincent


-- 
Vincent Hennebert                            Anyware Technologies
http://people.apache.org/~vhennebert         http://www.anyware-tech.com
Apache FOP Committer                         FOP Development/Consulting

Re: svn commit: r620277 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java

Posted by Andreas Delmelle <an...@telenet.be>.
On Feb 11, 2008, at 11:23, Vincent Hennebert wrote:

Hi Vincent

> Hi Andreas,
>
>> Author: adelmelle
>> Date: Sun Feb 10 05:01:07 2008
>> New Revision: 620277
> <snip/>
>> +    private NumberProperty(double num) {
>> +        //Store the number as an int or a long,
>> +        //if possible
>> +        if ((num >= 0 && num == Math.floor(num))
>> +                || num == Math.ceil(num)) {
>
> Isn’t that the same as
>     if (num == Math.floor(num))

Yes, for positive integers. For negative ints, it is 'num == Math.ceil 
(num)'.

Alternative would be: Math.abs(num) == Math.floor(num)

Cheers

Andreas

Re: svn commit: r620277 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/fo/properties/NumberProperty.java

Posted by Vincent Hennebert <vi...@anyware-tech.com>.
Hi Andreas,

> Author: adelmelle
> Date: Sun Feb 10 05:01:07 2008
> New Revision: 620277
<snip/>
> +    private NumberProperty(double num) {
> +        //Store the number as an int or a long,
> +        //if possible
> +        if ((num >= 0 && num == Math.floor(num))
> +                || num == Math.ceil(num)) {

Isn’t that the same as
    if (num == Math.floor(num))
? Or replace floor with ceil or rint, basically if the number actually 
is an integer, then the 3 methods should return a value equal to num 
itself.

Unless I missed something?

Vincent


-- 
Vincent Hennebert                            Anyware Technologies
http://people.apache.org/~vhennebert         http://www.anyware-tech.com
Apache FOP Committer                         FOP Development/Consulting