{"record":{"id":"65b27d20daf05ccc","repo":"apple/pkl","slug":"cannotconvertlargefloat","errorCode":"cannotConvertLargeFloat","errorMessage":"cannotConvertLargeFloat / cannotConvertNonFiniteFloat","messagePattern":"cannotConvertLargeFloat / cannotConvertNonFiniteFloat","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/binary/TruncatingDivisionNode.java","lineNumber":117,"sourceCode":"  }\n\n  @Specialization\n  protected long eval(VmDataSize left, VmDataSize right) {\n    // use same conversion strategy as add/subtract\n    if (left.getUnit().ordinal() <= right.getUnit().ordinal()) {\n      var leftValue = left.convertTo(right.getUnit()).getValue();\n      return doTruncatingDivide(leftValue, right.getValue());\n    }\n    var rightValue = right.convertTo(left.getUnit()).getValue();\n    return doTruncatingDivide(left.getValue(), rightValue);\n  }\n\n  private long doTruncatingDivide(double x, double y) {\n    try {\n      return MathUtils.roundToLong(x / y, RoundingMode.DOWN);\n    } catch (ArithmeticException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder()\n          .evalError(\n              Double.isFinite(x) ? \"cannotConvertLargeFloat\" : \"cannotConvertNonFiniteFloat\",\n              new ProgramValue(\"Float\", x))\n          .build();\n    }\n  }\n}\n","sourceCodeStart":99,"sourceCodeEnd":125,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/binary/TruncatingDivisionNode.java#L99-L125","documentation":"Thrown when the double-precision truncating division (`~/` on Float operands) produces a quotient that cannot be represented as a 64-bit Long: either a finite result too large (cannotConvertLargeFloat) or a non-finite result such as Infinity/NaN (cannotConvertNonFiniteFloat). MathUtils.roundToLong throws ArithmeticException in both cases.","triggerScenarios":"Evaluating `left ~/ right` where either operand is a Float and `x / y` overflows Long range, or is Infinity/NaN (e.g. division by 0.0, or infinite operand).","commonSituations":"Dividing by a Float zero, operating on huge Float config values, NaN propagating from an earlier Float computation.","solutions":["Check the divisor is non-zero before Float truncating division","Validate Float operands are finite and the quotient fits in Int range","Perform the division in Float and convert with an explicit range check","Fix upstream computations that produce NaN/Infinity"],"exampleFix":"// before\nparts = total ~/ stepSize // stepSize == 0.0\n// after\nparts = if (stepSize == 0.0) 0 else total ~/ stepSize","handlingStrategy":"validation","validationCode":"function safeFloatTruncDiv(x, y) { return (y === 0 || !Number.isFinite(x / y)) ? 0 : x / y }","typeGuard":"function isConvertibleToLong(q) { return Number.isFinite(q) && Math.abs(q) <= 9223372036854775807 }","tryCatchPattern":null,"preventionTips":["Check Float divisor is non-zero before `~/`","Ensure Float operands are finite","Range-check quotient before converting to Int"],"tags":["pkl","float","truncating-division"],"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-17T15:17:12.973Z"}