I think I found a bug on kotlin.js <https://try.ko...
# javascript
g
I think I found a bug on kotlin.js https://try.kotlinlang.org/#/UserProjects/7g3b3r1k6oqfq82ltabjfm44qt/1175fthomfiusfk2n0c268bqc4 When running on the JVM the double conversion to string is always good (with .0). Running as js, the conversion to string doesn’t put the “.0”
Copy code
fun main(args: Array<String>) {
    val dbl = 1.toDouble()
    println("M$dbl")  				// M1.0 OK
    println(moveTo(1.toDouble())) 	// M1   KO
}

fun moveTo(x:Double) = "M$x"
k
I don't think it's a bug. It's rather platform-specific behavour. It's just how Number.toString works in JavaScript
g
In both cases I use string interpolation on a double. Results are different. They shouldn’t.
Kind of problem it raises: the execution of tests in a common module fail on js because results are different than the jvm version.
k
JS and JVM backends are not 100% same
It's quite hard to decide what is a "platform-specific behavour" and what is not.
I suppose you have to rewrite your test to be more flexible. For example, you can cut ".0" suffix if it exists and use
assertEquals("1", x)
g
The problem I see: if you take my code and use “refactor-> introduce method” on third line, it won’t produce the same result.
k
What do you mean?
g
There is an incoherence between lines 3 and 4. They should produce the same result.
k
They should not
Ah, I see
Yes, the first line is resolved to a compile-time constant
And it's evaluated to string by compiler (on JVM side)
g
So it’s a bug, no?
k
Yes, it's a bug
g
Who create it? You will be more precise in its description I think.
k
Yes, I'll do that
g
👍
k
g
I don’t understand why you say the expected result should be M1.
It should be M1.0
The string representation of the double 1.0 is “1.0”
And it’s consistent with java.
i
Whether
Double.toString
should produce the exact same result on all platforms is a debatable question, please open a separate issue for that.
g
Ok
There is another weird case (even if I’m not sure it’s the same bug):
Copy code
fun main(args: Array<String>) {
    val result = -0.0
    println("$result") // -0.0                  
    log(result) 
}

fun log(x:Double) {
    println("$x") //0 <-- should be negative
}