Moreover, the replacements all have more digits.Įxample 7 should not have been flagged either, because the replacement is another 17-digit number which is further than the original from the floating-point number. When I recently encountered the Kotlin inspection I went back to that bug report and tried out some of its examples here are eight literals that the inspection flagged, along with their suggested replacements:Įxamples 1-6 should not have been flagged, since they have 15 digits or fewer. I learned about this bug over six years ago, when writing “Java Doesn’t Print The Shortest Strings That Round-Trip”. It has a 20-year old bug against it that is the source of the issue. Kotlin uses Java’s floating-point to decimal conversion routine, the dtoa() method of Java’s FloatingDecimal class. (The inspection was enabled by default for me.) Enabling the floating-point literal inspection. You can enable the inspection in IntelliJ IDEA by going to Preferences -> Editor -> Inspections -> Kotlin -> Other Problems and checking Floating-point literal exceeds the available precision. And no literal with 15 digits or fewer should ever be flagged, since doubles have of 15-digits of precision.īut IntelliJ doesn’t always adhere to that, like when it suggests an 18-digit replacement for a 13-digit literal! An 18-digit replacement suggested for a 13-digit literal! Literals with 16 or 17 digits should be flagged if there is a replacement that is shorter or closer. Hovering over a flagged 17-digit literal suggests a 10-digit replacement.įor Doubles for example, every literal over 17-digits should be flagged, since it never takes more than 17 digits to specify any double-precision binary floating-point value. It will suggest an equivalent literal (one that maps to the same binary floating-point number) that has fewer digits, or has the same number of digits but is closer to the floating-point number. IntelliJ IDEA has a code inspection for Kotlin that will warn you if a decimal floating-point literal exceeds the precision of its type ( Float or Double).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |