Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign.getInstance().longDoubleFromString returns garbage #60

Open
aploese opened this issue Dec 19, 2018 · 3 comments
Open

Foreign.getInstance().longDoubleFromString returns garbage #60

aploese opened this issue Dec 19, 2018 · 3 comments

Comments

@aploese
Copy link

aploese commented Dec 19, 2018

The failing of NumberTest.returnDefaultF128HighPrecision is buried here.
To reveal the reason, add the following test case to ForeignTest:
@Test public void longDoubleFromString() { String strValue = "1.234567890123456789"; BigDecimal value = new BigDecimal(strValue); assertEquals(value.toEngineeringString(), value.toString()); byte[] ld = new byte[Type.LONGDOUBLE.size()]; Foreign.getInstance().longDoubleFromString(value.toEngineeringString(), ld, 0, Type.LONGDOUBLE.size()); String strRetValue = Foreign.getInstance().longDoubleToString(ld, 0, 0); // String strRetValueE = Foreign.getInstance().longDoubleToEngineeringString(ld, 0, 0); BigDecimal retValue = new BigDecimal(strRetValue); BigDecimal delta = value.subtract(retValue).abs(); assertTrue("Not equals, expected: " + value.toEngineeringString() + " but was: " + retValue.toEngineeringString(), delta.compareTo(new BigDecimal("0.0000000000000000001")) < 0); }

I will poke in the c sources to figure out whats wrong...

@aploese
Copy link
Author

aploese commented Dec 20, 2018

The problem is, that the POSIX strtold uses the current locale to convert a string to long double, whereas BigDecimal.toEngeneeringString does not.
I could think of two solutions:

  1. use a java.text.Formatter to format and parse the BigDecimal
  2. set the locate thread local to "C" in the natve code with uselocale.
    doing this, it would be adviable to pass the BeigDecimal itself and do toEngeneeringString in the native part.

All other issues I saw I will file as separate issue.

@aploese
Copy link
Author

aploese commented Dec 20, 2018

Sorry, this is a duplicate of #51

@aploese
Copy link
Author

aploese commented Dec 20, 2018

Fixed this in https://github.com/aploese/jffi/tree/fix-mips (hotfix) currently it creates a new locale "C" for every call.
@headius Move this to the global JNI_OnLoad and JNI_OnUnLoad functions?

vaukai added a commit to vaukai/gentoo that referenced this issue Nov 20, 2021
Avoid jnr/jffi#60
1) returnDefaultF128HighPrecision(com.kenai.jffi.NumberTest)
java.lang.AssertionError

Package-Manager: Portage-3.0.28, Repoman-3.0.3
Signed-off-by: Volkmar W. Pogatzki <gentoo@pogatzki.net>
gentoo-bot pushed a commit to gentoo/gentoo that referenced this issue Nov 20, 2021
Avoid jnr/jffi#60
1) returnDefaultF128HighPrecision(com.kenai.jffi.NumberTest)
java.lang.AssertionError

Package-Manager: Portage-3.0.28, Repoman-3.0.3
Signed-off-by: Volkmar W. Pogatzki <gentoo@pogatzki.net>
Closes: #22916
Signed-off-by: Miroslav Šulc <fordfrog@gentoo.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant