Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Floating point numbers

The Java data types float and double are floating point data types which are represented in the JNI through the C data types jfloat and jdouble.

These data types are formally compatible with the C data types float and double. However, as they are represented in IEEE format (instead of /390 format) they cannot be used in C without taking precautionary measures.

As well as explicit conversion options, appropriate compiler and runtime system extensions are provided to support the IEEE format. These allow you to work directly with this number format in C.

Explicit conversion

A number of functions are available for explicit conversion between floating-point numbers in IEEE format and in /390 format. These are declared in the header file ieee_390.h, which is part of the CRTE distribution. These conversion functions are described in the manual “CRTE” [3].


Example

The following example shows the use of a conversion function in a native method which performs arithmetic manipulations on a floating point number. On the Java side the method will be declared as:

public native double manipulate(double arg);


The associated C program could look like this:

#include <jni.h>
#include ".....h" // javah generated Header
#include <ieee_390.h>
JNIEXPORT jdouble JNICALL
Java_..._manipulate(JNIEnv *env, jobject jthis, jdouble num);
{
  double result, arg;
  arg = ieee2double(num);
  result = (arg < 1.7)? arg * 3.4 : arg - 1.0;
  return double2ieee(result);
}

The above code example does not contain any error handling for possible conversion errors.

IEEE floating point numbers in the C code

As of version V3.0B, the C/C++ compiler allows you to generate code for IEEE format as an alternative to /390 format for floating point numbers. The setting, which is controlled via the compiler option -Kieee_floats, applies to the entire compilation unit (source file).

This option only has an effect on floating point constants in the source code, and on arithmetic, type conversion or comparison of floating point numbers. It has no effect on the passing of such data to other functions or simple assignments

Setting this option also has the effect of implicitly permitting the use of C library functions with floating point arguments and/or floating point result in a variant for IEEE arithmetic.

All the arithmetic is processed using corresponding emulation routines. This applies to SQ systems too, as long as generation of native code for the corresponding commands via Asstran is not possible. Naturally this has a negative effect on performance. C programs which make intensive use of floating point arithmetic should therefore not be run in this mode.


Example

The example shown above could then be implemented as follows:

#include <jni.h>
#include ".....h" // javah generated Header
JNIEXPORT jdouble JNICALL 
Java_..._manipulate(JNIEnv *env, jobject jthis, jdouble num) 
{ 
    return (num < 1.7)? num * 3.4 : num - 1.0;
}

The compilation must be carried out using the C compiler option -Kieee_floats.

IEEE floating point numbers in the C runtime system

The C runtime system contains, in addition to the conversion routines which are declared in the ieee_390.h, all the essential XPG4 functions which work with floating point numbers in a variant for IEEE arithmetic. When the aforementioned compiler option for using IEEE is selected, the corresponding library functions are normally used automatically without the user needing to do anything. You can also modify this behavior for mixed mode (see the manual “CRTE” [3]).


Example

The next example illustrates the use of the IEEE version of the C function tanh in a native method for calculating the hyperbolic tangent in a Java class. On the Java side the method will be declared as:

public native double tanhyp(double arg);

The associated C program could look like this:

#include <math.h>
#include <jni.h
#include ".....h" // javah generated Header 
JNIEXPORT jdouble JNICALL 
Java_..._tanhyp(JNIEnv *env, jobject jthis, jdouble num) 
{ 
    //printf("tan_hyp called with: %e\n",num); 
    return tanh(num); 
}

To work correctly it must naturally be compiled in this form using the C compiler option -Kieee_floats.