Support for hexadecimal floats

I don’t see why hexadecimal floats like 0x2.468p6 and 0x0.8421p5 where p is the exponent of 2 wouldn’t be added.
I’d like Kotlin to add hexadecimal floats along with binary floats.


fun main() {
    println(0x1.2345p6) // 72.8173828125
    println(0b1.101010p3) // 13.25

I can’t really come up with any argument that would be against this proposal. The only one I see is that I can’t think of any use case for this and it would be confusing to read for nearly everyone.

Also do you have any link that explains how those numbers work exactly? My guess is that it is something like

hexToDecimal(0xA.BpC) = (hexToDecimal(A) + (hexToDecimal(B) / 16^hexDigitCount(B))) * 2 ^ C
with A, B and C as hexadecimal numbers

In the end you should probably create a feature request at with an explanation of how the numbers work and as many usecases as you know of. That said, unless you have a very good usecase I don’t think this will be a priority and you might need to wait quite some time for this to be implemented in kotlin (if ever).
Also if you are confident enough you can try to write a compiler plugin that allows you to use such numbers. Even though those are still experimental and undocumented there are a number of talks/videos on how to use them (I think they will be released in a more official if still experimental form with kotlin 1.5). This might also help in getting this added to kotlin as a default.

1 Like

Given how many people seem to misunderstand what binary floating-point types are (viz. the number of questions that get asked about them…), and how often they’re used when totally unsuitable (e.g. for currency values), I’m very wary of providing any more ways to create them, or being seen to encourage their use at all!

(Also, I can’t recall a single case where I would ever have found a hex literal format for floats remotely useful.⠀I can’t help thinking that it must be a very niche use.)

If anything, I think we should be providing easier ways to create BigDecimals (which seem much more likely to be appropriate).⠀Especially since Kotlin makes them so natural and easy to use!


I have some code I’m looking at converting to Kotlin which wants these:

        /** Relative threshold for small singular values.  */
        private const val EPS: Double = 0x1.0p-52

        /** Absolute threshold for small singular values.  */
        private const val TINY: Double = 0x1.0p-966

My options are now:

  1. Pick new values in base 10 since the exact values may not actually matter.

  2. Use 2.0.pow(-52) to get the value, but for annoying reasons powers are not considered constants by the compiler.

I’ve also found a want for Double.MIN_NORMAL, which seems like it was arbitrarily dropped from Double.

The use case here is numerical calculation. I’d actually love to use fixed precision, but it’s far from usable today.