I'm trying to implement a fraction function in kot...
# kotlin-native
n
I'm trying to implement a fraction function in kotlin native (opposite of truncate), and given the following C code:
Copy code
#include <stdio.h>
float fract(float x) {
    return x - (long)x;
}
int main() {
    printf("%f", fract(2.3f));
}
with output
0.3
, I expected the same output from kotlin code:
Copy code
fun fract(x: Float): Float {
	return x - x.toLong()
}

fun main() {
	println(fract(2.3f))
}
instead I get:
0.29999995
What's happening here?
e
It's likely just the decimal precision setting for the strings generated by the print functions you're using. They may be different by default.
m
Correct, printf by default has a precision of 6 digits
You can also see in a debugger that in C it too has the same value
💯 2
n
Ooooooh. That makes so much sense. Thanks!