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

    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
    306

    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
    9

    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
    306

    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
    9

    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
    306

    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.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)