[x3d-public] X3DJSAIL -- precision stripping of trailing zeroes and decimal point

Don Brutzman brutzman at nps.edu
Tue Feb 7 23:50:15 PST 2017


p.s. this works satisfactorily because first converting an integer (such as 100) to String results in trailing decimal point and zero (such as 100.0) which then allows rest of regex filter to work.

alternate form might be to only apply the regex to numeric strings containing decimal points, but minimizing logic keeps performance high.

have checked some examples, seems to work OK.


On 2/4/2017 6:06 PM, Don Brutzman wrote:
> [cc: x3d public list]
>
> John, thanks for requesting that trailing zeros get stripped.  Now implemented using new utility function
>
>     http://www.web3d.org/specifications/java/javadoc/org/web3d/x3d/java/fields/X3DConcreteField.htmlstripTrailingZeroes-double-
>     http://www.web3d.org/specifications/java/javadoc/org/web3d/x3d/java/fields/X3DConcreteField.htmlstripTrailingZeroes-float-
>
> Surprisingly this took a little doing, apparently it is an edge case in Java.
>
> For anyone interested, useful (though high variance) references are
>
>     StackOverflow: Remove trailing zeros from double (String manipulation solution)
>     http://stackoverflow.com/questions/11284938/remove-trailing-zeros-from-double
>
>     StackOverflow: How to nicely format floating numbers to String without unnecessary decimal 0? ("In short" solution)
>     http://stackoverflow.com/questions/11284938/remove-trailing-zeros-from-double
>
> Looking for simplicity and high performance among various approaches, I adapted a regular expression (regex) version from one alternative within the first forum entry as
>
>     public static String stripTrailingZeroes (float value)
>     {
>         // regex to strip trailing zeroes, then strip trailing decimal point
>         return String.valueOf(value).replaceAll("[0]*$", "").replaceAll("\\.$", "");
>     }
>
> regex decipherment:
>
> - [0]* zero or more 0 characters
> - $    end of line anchor
> - \\   escaped, literal backslash character \
> - \\.  escaped, literal decimal point
>
> None of the code is profiled yet but no discernible slowdown seen - as expected, regex processing appears to be extremely efficient.
>
> Future work #1: we may need to coerce English locale inside X3DJSAIL in order to force the output of decimal points as period . instead of comma , (as is common in Europe).
>
> Future work #2: this pattern is good to add to our X3D Regex page when that starts moving again.
>
>     http://www.web3d.org/specifications/X3dRegularExpressions.html
>
> Future work #3: we should likely consider adding such stripping of trailing zeroes from numeric types as part of X3D Canonical Form since such characters are superfluous and do not change the graphical contents of a file.
>
>     ISO/IEC 19776-3:200x X3D Compressed binary encoding -- 4 Concepts
>     4.2.3 X3D canonical form
>     http://www.web3d.org/documents/specifications/19776-3/V3.1/Part03/concepts.html#X3DCanonicalForm
>
> Interestingly, stripping the trailing .0 characters seems to improve readability too.  Canonicalization (C14N) suggestion posted for eventual resolution using Mantis issue tracker.
>
> Test reports welcome.
>
>     http://www.web3d.org/specifications/java/X3dJavaSceneAuthoringInterface.html
>
> Have fun with X3DJSAIL Java!   8)
>
> On 2/3/2017 5:11 PM, Don Brutzman wrote:
>> you are right (of course) and i will look for the correct formatting invocation in Java to accomplish that approach.
>>
>> On 2/2/2017 5:28 AM, yottzumm at gmail.com wrote:
>>>>> Re: removing precision of .0 floats and doubles…often this precision is added by the software, so we would be restoring it.
>>>
>>> Don Brutzman wrote:
>>>
>>>> hmmm let's look closely at those please.  i think that precision is defined by data type so either 0 or 0. or 0.0 are equivalent.
>>>
>>>> stripping trailing zeroes reduces file size without loss of information.  We probably should add that to C14N binary encoding rules
>>>
>>> I suggest that the X3DJSAIL toString() field methods for floats and doubles (and their arrays, vectors, matrices, MF fields, etc) strip off trailing .0’s – Removing the .0 should save space if not anything else.  This will help with roundtrip comparisons.  I believe this is in many places, but I could be wrong.  Perhaps create a helper method which does the precision stripping.  In a more scientific sense, the precision is being added by Java I think…just need to use the right method for printing in out (in Number, perhaps?).  This is basically happening often when a float is encountered which is an integer.  If you want specifics, I will try to create diffs.
>
> all the best, Don


all the best, Don
-- 
Don Brutzman  Naval Postgraduate School, Code USW/Br       brutzman at nps.edu
Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149
X3D graphics, virtual worlds, navy robotics http://faculty.nps.edu/brutzman



More information about the x3d-public mailing list