soywiz-archive / jtransc

Bytecode to source converting Java & Kotlin code into JavaScript, C++, D, C#, PHP, AS3, Dart and Haxe and run it everywhere. Also use JVM code in your favourite language as a library.
https://jtransc.soywiz.com/
Apache License 2.0
632 stars 67 forks source link

String.format(Float) not work #244

Closed ghost closed 7 years ago

ghost commented 7 years ago

String.format("val=%.2f, normal=%.2f, min=%.2f, max=%.2f, ", getProgressValue(), getProgressNormal(), getProgressMin(), getProgressMax()) If any where return Float.NaN always crash.

I try make as in Android

soywiz commented 7 years ago

In which targets is this reproduced? Can you add a PR with two commits: one enabling a test where is this reproduced (and fails in travis) and another commit disabling it so we can merge and fix it later? I can check it today or this weekend

ghost commented 7 years ago

Yes, i try reproduce in first step. Created ticket firstly, for bug naming =) maybe in next time need better name, not Issue244 =)))

ghost commented 7 years ago

BTW, haxe have impl already, need mapping http://api.haxe.org/Math.html#NEGATIVE_INFINITY

ghost commented 7 years ago

I make 100 test try understands why my app crach =) And finally i find it: int i = (int) Float.NaN @soywiz need your help, i make commit soon with new test. I understand first problem with string, but not understand how fix second.

ghost commented 7 years ago

java

2147483647
-2147483648
0

jtransc

0
0
-2147483648
soywiz commented 7 years ago

Float to int conversions in haxe happens here: https://github.com/jtransc/jtransc/blob/bd0a45d3d39b88e8f4e35455bd4d356e33e2a04b/jtransc-rt/resources/hx/N.hx#L199

But is it a crash? Or just an undefined behaviour? Or that 0 happens to convert into a division by zero?

ghost commented 7 years ago

I can not catch in MSVC, only comment part of code and test =( Because reflection in project very hard, find too very hard. I think return (value - min) / (max - min); if default value will be +INF and -INF then in java / 2+INF in jtransc / 0

soywiz commented 7 years ago

Do you have the native crash report from android? Maybe the error says something relevant. Whether it is just an exception uncatched (stdout should note that), or a memory crash or division by 0 crash or things like that. Maybe something like this would work? Or logging via adb https://play.google.com/store/apps/details?id=com.nolanlawson.logcat&hl=es

We can try to test edge cases related with nans, infinites, divisions, and so on.

For example idiv, it is know to crash in some edge cases, that are covered here: https://github.com/jtransc/jtransc/blob/bd0a45d3d39b88e8f4e35455bd4d356e33e2a04b/jtransc-rt/resources/hx/N.hx#L217

But didn't know float operations caused that too.

soywiz commented 7 years ago

Another step can be modify N.hx :: N.f2i to add ifs to handle infinites like Java do

ghost commented 7 years ago

Ok, i try do it.

soywiz commented 7 years ago

I have added support for %.2f in formatter and unified (int)floatValue, (int)doubleValue. By mistake I pushed it to master directly, now I have set the config so I cannot push to master directly.

https://github.com/jtransc/jtransc/commit/a522fd0ae61e0418b4e1be840fde804721294e34 https://github.com/jtransc/jtransc/commit/79fc7b04c55d3d68c9642bca3eb6a95c5ade40a3 https://github.com/jtransc/jtransc/commit/69310eed24a23b69442981506ed9b591c5c06a92