{"record":{"id":"89969491935862bb","repo":"apple/pkl","slug":"cannotconvertlargefloat-cannotconvertnonfinitefloa","errorCode":null,"errorMessage":"cannotConvertLargeFloat|cannotConvertNonFiniteFloat (conditional)","messagePattern":"cannotConvertLargeFloat\\|cannotConvertNonFiniteFloat \\(conditional\\)","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/VmSafeMath.java","lineNumber":122,"sourceCode":"      CompilerDirectives.transferToInterpreter();\n      throw new VmExceptionBuilder().evalError(\"intValueTooLarge\", x).build();\n    }\n  }\n\n  public static double truncate(double x) {\n    if (x < 0) {\n      return StrictMath.ceil(x);\n    }\n    return StrictMath.floor(x);\n  }\n\n  @TruffleBoundary\n  public static long toInt(double x, Node sourceNode) {\n    try {\n      return MathUtils.roundToLong(x, RoundingMode.DOWN);\n    } catch (ArithmeticException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw new VmExceptionBuilder()\n          .withLocation(sourceNode)\n          .evalError(\n              Double.isFinite(x) ? \"cannotConvertLargeFloat\" : \"cannotConvertNonFiniteFloat\",\n              new ProgramValue(\"Float\", x))\n          .build();\n    }\n  }\n\n  public static long increment(long x) {\n    try {\n      return Math.incrementExact(x);\n    } catch (ArithmeticException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw intOverflow();\n    }\n  }\n\n  public static long decrement(long x) {","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/VmSafeMath.java#L104-L140","documentation":"`VmSafeMath.toInt` truncates a Double toward zero with `RoundingMode.DOWN` and converts to long. It fails when the Float is too large to fit in a Long (cannotConvertLargeFloat) or is NaN/Infinity (cannotConvertNonFiniteFloat), both surfaced as ArithmeticException from MathUtils.roundToLong.","triggerScenarios":"Calling `Float.toInt()` (or stdlib functions that require an Int from a Float) where the Double is >= 2^63, <= -2^63, NaN, +Infinity or -Infinity.","commonSituations":"Division producing Infinity (divide by zero on floats), NaN from invalid math (sqrt of negative), or huge computed magnitudes from exponentiation.","solutions":["Validate the Float is finite and within Long range before converting: `x.isFinite && x >= -9.223372036854776E18 && x < 9.223372036854776E18`.","Fix upstream arithmetic that yields NaN/Infinity (guard division denominators, clamp intermediate results).","Keep the value as a Float if integer precision is not required.","Clamp or scale the value so it fits in the integer range."],"exampleFix":"// before\nval n = (1.0e300 / 1.0e-300).toInt()\n// after\nval raw = 1.0e300 / 1.0e-300\nval n = if (raw.isFinite && raw < 9.223372036854776E18) raw.toInt() else Long.MAX_VALUE","handlingStrategy":"validation","validationCode":"function canConvertToInt(x) { return Number.isFinite(x) && x > -9.223372036854776e18 && x < 9.223372036854776e18; }","typeGuard":"function isConvertibleFloat(x) { return typeof x === 'number' && Number.isFinite(x) && Math.abs(x) < 9.223372036854776e18; }","tryCatchPattern":"try { x.toInt() } catch (e) { /* Float too large or non-finite; handle/clamp */ }","preventionTips":["Guard division denominators to avoid Infinity","Check for NaN before converting floats","Clamp extreme magnitudes before integer conversion"],"tags":["pkl","float","conversion","range"],"backgroundTag":"value-out-of-range","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}