The Artima Developer Community
Sponsored Link

Artima Developer Spotlight Forum
Elliotte Rusty Harold on New Additions to java.lang.Math

8 replies on 1 page. Most recent reply: Nov 11, 2008 11:08 AM by Cameron Purdy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Elliotte Rusty Harold on New Additions to java.lang.Math Posted: Nov 4, 2008 10:32 PM
Reply to this message Reply
Advertisement

Stating that,

The most efficient code is the code you never write. Don't do for yourself what experts have already done,

Elliotte Rusty Harold reviews new mathematical functions in java.lang.Math in Java's new math.

The latest two version of the JDK each added 10 new methods to java.lang.Math. One reason those methods are important learn about is because they are almost certain to provide more efficient, and more accurate, implementations than what most developers would write themselves for certain calculations. In additions, many methods in java.lang.Math take advantage of native code that, in turn, exploits hardware acceleration on modern CPUs.

In the article, Harold notes many instances where a naive approach could lead to incorrect calculations, starting with such basic notions as the size of a number:

The Platonic ideal of the number is infinitely precise, while the Java representation is limited to a fixed number of bits. This is important when you deal with very large and very small numbers. For example, the number 2,000,000,001 (two billion and one) can be represented exactly as an int, but not as a float. The closest you can get in a float is 2.0E9 — that is, two billion.

Proper calculations of sine and other functions that are both accurate and fast require very careful algorithms designed to avoid accidentally turning small errors into large ones. Often these algorithms are embedded in hardware for even faster performance.

For example, almost every X86 chip shipped in the last 10 years has hardware implementations of sine and cosine that the X86 VM can just call, rather than calculating them far more slowly based on more primitive operations. HotSpot takes advantage of these instructions to speed up trigonometry operations dramatically.

In the rest of his article, Harold reviews some of the most useful new java.lang.Math methods, such as Math.hypot that calculates the Pythagorean equation:

Java 5 added a Math.hypot function to perform exactly this calculation, and it's a good example of why a library is helpful. The naive approach would look something like this:

public static double hypot(double x, double y){
  return Math.sqrt (x*x + y*y);
}

The actual code [as implemented in java.lang.Math] is somewhat more complex... The first thing you'll note is that this is written in native C code for maximum performance. The second thing you should note is that it is going to great lengths to try to minimize any possible errors in this calculation. In fact, different algorithms are being chosen depending on the relative sizes of x and y.

The latest version of java.lang.Math also corrects naming issues with the log method:

Logs base 10 tend to appear in engineering applications. Logs base e (natural logarithms) appear in the calculation of compound interest, and numerous scientific and mathematical applications. Logs base 2 tend to show up in algorithm analysis.

The Math class has had a natural logarithm function since Java 1.0. That is, given an argument x, the natural logarithm returns the power to which e must be raised to give the value x. Sadly, the Java language's (and C's and Fortran's and Basic's) natural logarithm function is misnamed as log(). In every math textbook I've ever read, log is a base-10 logarithm, while ln is a base e logarithm and lg is a base-2 logarithm. It's too late to fix this now, but Java 5 did add a log10() function that takes the logarithm base 10 instead of base e... Math.log10() has the usual caveats of logarithm functions: taking the log of 0 or any negative number returns NaN.

Harold also explains the Math.cbrt(), the hyperbolic trigonometric functions Math.cosh(), Math.sinh(), and Math.tanh(), as well as Math.signum.

What do you think of the latest math-related functions in Java?


Hoang Vu Nguyen

Posts: 3
Nickname: vu64
Registered: Apr, 2008

Re: Elliotte Rusty Harold on New Additions to java.lang.Math Posted: Nov 5, 2008 2:39 AM
Reply to this message Reply
I wonder why people who want to do Math want to do it in Java while Java has to do it in C.

Scott Peacock

Posts: 10
Nickname: speacock
Registered: Nov, 2007

Re: Elliotte Rusty Harold on New Additions to java.lang.Math Posted: Nov 5, 2008 9:32 AM
Reply to this message Reply
Because now they can do it in C... but in Java ;-)

If there's a library which makes a language a viable option where it wasn't before, that can only be a good thing.

Tkel Weizen

Posts: 1
Nickname: tkelechogi
Registered: Nov, 2008

Re: Elliotte Rusty Harold on New Additions to java.lang.Math Posted: Nov 5, 2008 10:18 AM
Reply to this message Reply
That's like asking, "Why don't they build airplanes out of the material that they build the black box out of?"

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: Elliotte Rusty Harold on New Additions to java.lang.Math Posted: Nov 5, 2008 2:59 PM
Reply to this message Reply
If the performance of a Math method is deemed important then the JVM implementer can transparently replace the C implementation with an intrinsic form. This already happens with methods like Math.sin()

Burkhard Neppert

Posts: 3
Nickname: hephaistos
Registered: Jun, 2007

Re: Elliotte Rusty Harold on New Additions to java.lang.Math Posted: Nov 10, 2008 9:18 AM
Reply to this message Reply
> The latest version of <code>java.lang.Math</code> also
> corrects naming issues with the <code>log</code>
> method:</p>
>
> Sadly, the Java language's (and C's and
> Fortran's and Basic's) natural logarithm function is
> misnamed as log(). In every math textbook I've ever read,
> log is a base-10 logarithm, while ln is a base e logarithm
> and lg is a base-2 logarithm. It's too late to fix this
> now, but Java 5 did add a log10() function that takes the
> logarithm base 10 instead of base e... Math.log10() has
> the usual caveats of logarithm functions: taking the log
> of 0 or any negative number returns NaN. </p>
>

Actually calling the logarithm base e "log" is not misnamed. There exist different naming conventions.

See e.g. the wikipedia entry for logarithm or
http://mathworld.wolfram.com/Logarithm.html
"Note that while logarithm base 10 is denoted log(x) in this work, on calculators, and in elementary algebra and calculus textbooks, mathematicians and advanced mathematics texts uniformly use the notation log(x) to mean ln(x), and therefore use log_(10)x to mean the common logarithm. Extreme care is therefore needed when consulting the literature."

Cameron Purdy

Posts: 186
Nickname: cpurdy
Registered: Dec, 2004

Re: Elliotte Rusty Harold on New Additions to java.lang.Math Posted: Nov 10, 2008 4:23 PM
Reply to this message Reply
Now if we can just get an intrinsic IEEE 754 decimal type ;-)

Peace,

Cameron Purdy | Oracle
http://www.oracle.com/technology/products/coherence/index.html

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Elliotte Rusty Harold on New Additions to java.lang.Math Posted: Nov 10, 2008 5:34 PM
Reply to this message Reply
> Now if we can just get an intrinsic IEEE 754 decimal type
> ;-)

Heresy! Everyone knows that real programmers use floating point for everything.

Cameron Purdy

Posts: 186
Nickname: cpurdy
Registered: Dec, 2004

Re: Elliotte Rusty Harold on New Additions to java.lang.Math Posted: Nov 11, 2008 11:08 AM
Reply to this message Reply
I don't mind the point floating, but I am concerned about the radix.

Peace,

Cameron Purdy | Oracle
http://www.oracle.com/technology/products/coherence/index.html

Flat View: This topic has 8 replies on 1 page
Topic: Dmitry Jemerov on IntelliJ IDEA 8 and RubyMine Previous Topic   Next Topic Topic: Deepa Sobhana on the Servlet 3.0 Specification


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us