CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2013
    Posts
    31

    Alternative Java Virtual Machines and Floating Point

    Languages other than Java seem to allow options to disable floating point underflow and overflow. Even languages with memory managers at runtime.

    -Can someone tell me of a Java Virtual Machine that runs SE Java, at least version 5 or later, in 64 bit, on Windows, that does not allow overflow or underfloat of float or double floating point arithmetic?

    -Are there other JVMs that are 64 bit and have don't allow overflow or underfloat of float or double floating point arithmetic on Mac? On Linux?

    -What strict accurate floating point Java Virtual Machines are there?

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: Alternative Java Virtual Machines and Floating Point

    Quote Originally Posted by Zachary1234 View Post
    Can someone tell me of a Java Virtual Machine that runs SE Java, at least version 5 or later, in 64 bit, on Windows, that does not allow overflow or underfloat of float or double floating point arithmetic?
    What do you mean by "does not allow"? It's not the programming language that causes overflow/underflow, it's the programmer.

    The Java language is very well specified and almost nothing is implementation defined. This is to ensure portability, one of the most important aspects of Java. It means that every JVM will run a certain Java program (compiled to bytecode) exactly the same. How is defined by the Java specification. This is what it says about floating points,

    https://docs.oracle.com/javase/specs...html#jls-4.2.3

    If this isn't to your liking your only option is to use an alternative floating point library. If none can be found you can write one yourself. In that case you would use the Java JNI interface to access code written in some other language, like say C.

    An important lesson here is that Java is very easy to use as long as you stay inside the Java environment, but if you need to go outside things may quickly become very difficult. This is important to have in mind before using Java for a project.
    Last edited by wolle; March 12th, 2018 at 10:25 AM.

  3. #3
    Join Date
    Jul 2013
    Posts
    31

    Re: Alternative Java Virtual Machines and Floating Point

    -Well. I am aware of all this, howeve the crux of my question is:

    consider an example as follows:

    Double a = 0.1;

    Double b = 0.1;

    Double x = a*b;

    out.println(x == 0.01); //prints false

    -Are there other JVMS that do not adhere to the Java Language Specification in this area?
    -Are there other JVMs that are 64 bit and have don't allow overflow or underfloat of float or double floating point arithmetic on Mac? On Linux?

    If so, what are they?

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: Alternative Java Virtual Machines and Floating Point

    Quote Originally Posted by Zachary1234 View Post
    , howeve the crux of my question is:
    The crux you're addressing has nothing to do with Java or any other programming language. It's a fundamental problem that arises because a number that can be represented with a limited number of digits in one number system may require an infinite number of digits in another.

    For example 0.1 in decimal is 0.00011001100110011001100110011001100... (continues forever) in binary. Since a floating point only has a limited memory space this infinite sequence of binary digits has to be cut short at some point. The result is that the decimal number 0.1 does not have an exact representation as a floating point. That's the crux.

    Now for example the decimal number 0.125 actually has an exact floating point representation which is 0.001. So if you do,

    Code:
    Double a = 0.125;
    Double b = 0.125;
    Double x = a*b;
    System.out.println(x == 0.015625); // Now prints true
    then true will be printed.

    Problems associated with the fact that the floating point type has a limited size are called rounding errors. Another example of that would've been if the result of the a*b multiplication 0.015625 didn't fit within the memory space of a floating point and therefore some bits had to be discarded.

    There are several ways programmers deal with rounding errors of floating points: Never compare floating point numbers for equality using ==. Never use floating point numbers to represent money. Never add up the same floating point number in a loop. Etcetera.

    Most languages use the IEEE 754 standard for floating points.

    https://en.wikipedia.org/wiki/IEEE_754

    As long as this standard is used you will have the above mentioned problems. Resistance is futile . But there is another standard that is different. It's called Decimal Floating Point (IEEE 754-2008),

    https://en.wikipedia.org/wiki/Decimal_floating_point

    If you can find a language or technology or package that supports this it may be a solution. I don't know the true nature of your problem so I cannot tell.
    Last edited by wolle; March 14th, 2018 at 12:03 AM.

  5. #5
    Join Date
    Jul 2013
    Posts
    31

    Re: Alternative Java Virtual Machines and Floating Point

    Yes, I know about this. I also know the difference between float and double.

    Thing is, is that there are C++ language versions that allow me to turn these leaking phenomena off.

    /fp: strict
    /fp: precise

    Are there any multi-platform build version of a JVM that include a way to achieve this with float, Float, double, Double?
    If yes, what are they?

  6. #6
    Join Date
    Feb 2017
    Posts
    677

    Re: Alternative Java Virtual Machines and Floating Point

    Are there any multi-platform build version of a JVM that include a way to achieve this with float, Float, double, Double?
    If yes, what are they?
    Again, to the best of my knowledge the answer is no. A JVM cannot do this since Java is highly specified with almost nothing left to the Java implementation to decide.

    The Java language does have a strictpf keyword. It's normally used by the programmer in the Java source code but it may be possible to specify it to some Java compiler as a directive (just like the /fp: directives you can give to the Microsoft C++ compiler).

    I really don't know what "leaking phenomena" you hope to "turn off" but it would be easy to write a small Java test program using the strictfp keyword to investigate whether it solves your problem.

    My advice is that you take a step back and use floating points properly, solving your problems (whatever they are) from the ground up.

    Good luck.
    Last edited by wolle; March 13th, 2018 at 11:57 PM.

  7. #7
    Join Date
    Jul 2013
    Posts
    31

    Re: Alternative Java Virtual Machines and Floating Point

    -What I am looking for is a programmer who can do the (at least equivalent) of putting in
    Decimal Floating Point (IEEE 754-2008) for double and float arithmetic operations, for me,
    certainly for

    https://jdk.java.net/11/

    for me and/or the public. I don't really need a two mode version, just one that
    maintains range accurate operations to the result, always including accuracy
    for the final double or float digit. Is there someone out there who could take this on?

  8. #8
    Join Date
    Feb 2017
    Posts
    677

    Re: Alternative Java Virtual Machines and Floating Point

    Quote Originally Posted by Zachary1234 View Post
    -What I am looking for is a programmer who can do the (at least equivalent) of putting in
    Decimal Floating Point (IEEE 754-2008) for double and float arithmetic operations,
    There are libraries available like this from Intel,

    https://software.intel.com/en-us/art...t-math-library

    and Java has a low level interface JNI,

    https://www3.ntu.edu.sg/home/ehchua/...Interface.html

    so it's more or less an integration project.

    Maybe this integration into Julia could serve as an example,

    https://github.com/JuliaMath/DecFP.jl

    I suggest you get in touch with the person stated as Technology Contact in the Intel link,

    Good luck!
    Last edited by wolle; February 28th, 2019 at 12:34 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured