Comparing the Built-in Math Capability of C, C, Fortran, and Java


Comparing the Built-in Math Capability of C, C++, Fortran, and Java

There are some in the non-Java programming world who sniff that Java is only good for creating applets or perhaps business- related programs. They point to the supposedly superior math libraries of a language like C, C++, or Fortran. Let's do a one-to-one comparison of the math capabilities between Java and each of the other languages to see what the truth really is.

First let's compare the built-in math capabilities of Java and C, shown in Table 16.1.

Looking at Table 16.1 we can see that there is a lot of similarity between the C and Java math libraries. Java has the same absolute value, power, square root, exponential, natural logarithm, trigonometric, and inverse trigonometric capability. Many of the method names are the same too. In some ways the Java math libraries are more compact. C has three absolute value functions, one each for int , long , and double data types. Because Java supports method overloading, Java only needs one absolute value method that can accept int , long , float , and double type arguments.

It's true that the C libraries have some functionality that is not available with Java. Java does not have base 10 logarithm or the hyperbolic trigonometry methods in its math libraries. Nor does Java have the fractional splitting methods defined in the C libraries. On the other hand, the C libraries do not contain the min() or max() functions. The degrees-to-radians, radians-to-degrees, and rectangular-to-polar conversion methods are not available in C.

Table 16.1. Comparing C and Java Math Capabilities

C

J AVA

D ESCRIPTION

abs(), fabs(), labs()

abs()

Absolute value

pow()

pow()

Power

sqrt()

sqrt()

Square root

exp()

exp()

Exponential

log()

log()

Natural logarithm

log10()

N/A

Base 10 logarithm

acos(), asin(), atan()

acos(), asin(), atan()

Inverse trigonometric

cos(), sin(), tan()

cos(), sin(), tan()

Trigonometric

cosh(), sinh(), tanh()

N/A

Hyperbolic trig

N/A

min(), max()

Minimum/Maximum

atan2()

toDegrees(), toRadians(), atan2()

Unit conversion

ceil(), floor(), fmod()

ceil(), floor(), rint(), round()

Rounding

frexp(), ldexp(), modf()

N/A

Fractional splitting

div(), ldiv()

IEEERemainder()

Remainder

rand(), srand ()

random()

Random number

The built-in C++ math capability closely mirrors that of C including the hyperbolic trigonometric and base 10 logarithm functions. The C++ math libraries use method overloading to remove the need for separate functions to handle different argument types. The intrinsic math capability of Fortran is not markedly different from that offered by the other three languages, but over the years a number of commercially available libraries have been developed that substantially increase the Fortran math capability. The Numerical Algorithms Group (NAG) Fortran 90 Library, for example, offers functions that compute gamma functions, Bessel functions, error functions, Fresnel integrals, and elliptic integrals, to name a few.

All in all, Java math libraries hold their own when compared to the capabilities offered by C and C++ and for most standard mathematical computations also compare well against the Fortran math capability. In the next chapter, we will learn how you can define your own math methods to extend the built-in capabilities of the core Java libraries.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net