Java MemoryLayout/ValueLayout Questions.

Joined
May 31, 2022
Messages
6
Reaction score
0
Can I use the preview MemoryLayout/ValueLayout API, with its associated classes and interfaces, to adjust how float and double
do their arithmetic to results? I want to acheive something like the following:

Code:
float     == [int].[int]
double    == [long].[long]

where the Right Hand Side represents value formatting around the decimal/hexadecimal values around the dot separator point,
and the Left Hand Side represents floating point values themselves.

https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/foreign/MemoryLayout.html

I am having trouble working out and experimenting with this preview API. Is there someone on these forums who can tell me
if and how I can leverage this Java API so that I can systematically eliminate all base 10 and base 16 results from code like this:

Code:
public static void main (String ... args)
{
float a = 0.1F;
float b = 0.1F;
float c = a*b;
System.out.prinln(c);  //should output 0.01 only.
double d = 0.1D;
double e = 0.1D;
double g = a*b;
System.out.prinln(g);  //should output 0.01 only.
}

?
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
Can I use the preview MemoryLayout/ValueLayout API, with its associated classes and interfaces, to adjust how float and double
do their arithmetic to results? I want to acheive something like the following:

Code:
float     == [int].[int]
double    == [long].[long]

where the Right Hand Side represents value formatting around the decimal/hexadecimal values around the dot separator point,
and the Left Hand Side represents floating point values themselves.

https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/foreign/MemoryLayout.html

I am having trouble working out and experimenting with this preview API. Is there someone on these forums who can tell me
if and how I can leverage this Java API so that I can systematically eliminate all base 10 and base 16 results from code like this:

Code:
public static void main (String ... args)
{
float a = 0.1F;
float b = 0.1F;
float c = a*b;
System.out.prinln(c);  //should output 0.01 only.
double d = 0.1D;
double e = 0.1D;
double g = a*b;
System.out.prinln(g);  //should output 0.01 only.
}

?
No, the MemoryLayout/ValueLayout API does not provide a way to change the underlying representation of floating point values in Java or the way arithmetic is performed on them. It is primarily used for working with low-level memory, such as native memory allocated by C or C++ libraries.

As for the issue with the code you provided, it is a well-known problem in computer science that floating-point values cannot always exactly represent all real numbers, especially when they have a repeating decimal representation. So, the result of 0.1F * 0.1F is not exactly 0.01 in floating-point arithmetic, leading to an inaccurately rounded result. You might consider using BigDecimal or a custom solution that avoids floating point arithmetic for accurate decimal arithmetic in Java.
 
Joined
May 31, 2022
Messages
6
Reaction score
0
-Forgetting the StrictMath mapping to the fdlibm library that presently exists, is there any Java known way to
re-do, change or override the present way that floating point arithmetic is done with float and double operators,
in OpenJDK version 19?

-When all phases of Java Valhalla have been included in a release version of the OpenJDK, is there going to be, either
as part of Valhalla or alongside it, any way to change or very easily override the present way that floating point arithmetic
is done with float and double?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,560
Members
45,035
Latest member
HoTaKeDai

Latest Threads

Top